74
edits
No edit summary |
No edit summary |
||
Line 65: | Line 65: | ||
</pre> | </pre> | ||
TwitterBot :MID TERM ASSIGNMENT | |||
======Kaleidoscope Bot======= | |||
code for the image processing | |||
============================= | |||
from PIL import Image, ImageDraw,ImageFilter | |||
import PIL.ImageOps as im | |||
import numpy as np | |||
def makekaleido(): | |||
#put image path | |||
imgfile = "img.jpg" | |||
#load Image | |||
source = Image.open(imgfile) | |||
#adjust the source image a bit for nicer result | |||
base = source.rotate(31) | |||
#creat mask base the same size with source image | |||
mask = Image.new('RGBA', base.size, (255,255,255,0)) | |||
x, y = base.size | |||
# one variable to vary the size of triangle | |||
var = y/14 | |||
# Define triangle mask position (triangle with 20 degree) | |||
(originx,originy) = (x/3,int(0.9*y)) | |||
trih = int(12*var) #fix formular for triangle height | |||
triw = int(4.2*var) #fix formular for triangle width | |||
polygonpos = [(originx,originy), | |||
(originx+triw,originy), | |||
(originx+triw/2,originy-trih)] | |||
#print(trih,triw) | |||
# Create mask | |||
draw = ImageDraw.Draw(mask,'RGBA') | |||
draw.polygon(polygonpos,(0,0,0,255)) | |||
del draw | |||
mask.save("mask.png") | |||
# Get the Alpha band from the template | |||
tmplt = Image.open('mask.png') | |||
A = tmplt.split()[3] | |||
#make one piece of triangle on transparent bg | |||
[R,G,B]=base.split() | |||
tri = Image.merge('RGBA', (R, G, B, A)) | |||
#crop it to the exact size of triangle!! to create primary pattern | |||
#box (left, top , right, buttom) | |||
box =(originx,(originy-trih),(originx+triw),originy) | |||
pattern_plain=tri.crop(box) | |||
pattern_plain.save('pattern_plain.png') | |||
print('....pattern created....') | |||
# add style to pattern | |||
pattern = pattern_plain | |||
#pattern= pattern_plain.filter(ImageFilter.EDGE_ENHANCE) | |||
pattern.save('pattern_tri.png') | |||
print('....stylized pattern....') | |||
#make square canvas for the output (wide = double size of height of primary pattern) | |||
canvas =Image.new('RGBA',(2*trih,2*trih), (255,255,255,0)) | |||
canvas.save('tmpcanvas.png') | |||
pcanvas=Image.new('RGBA',(2*trih,2*trih), (255,255,255,0)) | |||
#put pattern on the canvas | |||
#make sure to put the tip of the triangle at the center of the canvas | |||
#because when we rotate the center of the object is the pivot point | |||
#note: paste command require the coordinate of top left corner | |||
#so point to paste the pattern is . . . | |||
ccenterx = int(trih-triw/2) | |||
canvas.paste(pattern,(ccenterx,trih)) | |||
# start rotate the pattern around every 40 degree | |||
for i in range (0,360,40): | |||
tmpcanvas = canvas | |||
tmppat = canvas.rotate(i) | |||
canvas= Image.alpha_composite(tmpcanvas,tmppat) | |||
# now we get half of the things | |||
half = canvas | |||
#mirror the half and put in the space to create simple kaleidoscpoe effect | |||
mirror = im.mirror(half) | |||
half2= mirror.rotate(20) | |||
#merge 2 half | |||
output=Image.alpha_composite(half,half2) | |||
output.save("final.png") | |||
if __name__ == '__main__': | |||
makekaleido() |
edits