# -*- coding: utf-8 -*-<br />#匯入三個模組<br />import Image,ImageDraw,ImageFont<br />import random<br />import math<br />'''準系統'''<br />#圖片寬度<br />width = 100<br />#圖片高度<br />height = 40<br />#背景顏色<br />bgcolor = (255,255,255)<br />#產生背景圖片<br />image = Image.new('RGB',(width,height),bgcolor)<br />#載入字型<br />font = ImageFont.truetype('FreeSans.ttf',30)<br />#字型顏色<br />fontcolor = (0,0,0)<br />#產生draw對象,draw是一些演算法的集合<br />draw = ImageDraw.Draw(image)<br />#畫字型,(0,0)是起始位置<br />draw.text((0,0),'1234',font=font,fill=fontcolor)<br />#釋放draw<br />del draw<br />#儲存原始版本<br />image.save('1234_1.jpeg')<br />'''示範扭曲,需要建立一個圖片對象'''<br />#新圖片<br />newImage = Image.new('RGB',(width,height),bgcolor)<br />#load像素<br />newPix = newImage.load()<br />pix = image.load()<br />offset = 0<br />for y in range(0,height):<br /> offset += 1<br /> for x in range(0,width):<br /> #新的x座標點<br /> newx = x + offset<br /> #你可以試試如下的效果<br /> #newx = x + math.sin(float(y)/10)*10<br /> if newx < width:<br /> #把源像素通過位移到新的像素點<br /> newPix[newx,y] = pix[x,y]<br />#儲存扭曲後的版本<br />newImage.save('1234_2.jpeg')<br />'''形變一下'''<br />#x1 = ax+by+c<br />#y1 = dx+ey+f<br />newImage = image.transform((width+30,height+10), Image.AFFINE, (1,-0.3,0,-0.1,1,0))<br />newImage.save('1234_3.jpeg')<br />'''畫幹擾線,別畫太多,免得使用者都看不清楚'''<br />#建立draw,畫線用<br />draw = ImageDraw.Draw(newImage)<br />#線的顏色<br />linecolor= (0,0,0)<br />for i in range(0,15):<br /> #都是隨機的<br /> x1 = random.randint(0,width)<br /> x2 = random.randint(0,width)<br /> y1 = random.randint(0,height)<br /> y2 = random.randint(0,height)<br /> draw.line([(x1, y1), (x2, y2)], linecolor) </p><p>#儲存到本地<br />newImage.save('1234_4.jpeg')<br />