標籤:字型下載 儲存 write text 座標 擷取 def lte 根據
69206210
今天抽空學習了一下之前瞭解過的pillow庫,以前看到的記得這個庫可以給圖片上加文字加數字,還可以將圖片轉化成字元畫,不過一直沒有找時間去學習一下這個模組,由於放假不用訓練,所以就瞎搞了一下
0、工欲善其事,必先利其器
關於pillow庫的安裝有幾種方式
0、使用pip安裝
$ pip install pillow
1、使用easy_install
$ easy_install pillow
2、通過pycharm安裝
1、學習並使用pillow庫
#匯入模組from PIL import Image#讀取檔案img = Image.open(‘test.jpg‘)#儲存檔案#img.save(filename,format)img.save(filename,"JPEG")#擷取圖片大小(width,height) = img.size#擷取圖片的源格式img_format = img.format#擷取圖片模式,有三種模式:L(灰階映像),RGB(真彩色)和CMYK(pre-press映像)img_mode = img.mode#圖片模式的轉換img = img.convert("L") #轉化成灰階映像#擷取每個座標的像素點的RGB值r,g,b = img.getpixel((j,i))#重設圖片大小img = img.resize(width,height)#建立縮圖img.thumbnail(size)
2、實戰演練將圖片轉化成字元畫,效果如下:
其實應該很容易想到,如果要達到這種效果,應該能想得到就是擷取圖上每一點的RGB值,然後根據這三種值確定這一點採用什麼字元,其實根據RGB來確定的交灰值,所以可以將圖片轉化成灰階圖片,來直接擷取每一點的灰階,或者通過灰階的轉換公式來使得RGB三值轉化成灰階
#coding:utf-8from PIL import Image#要索引的字元列表ascii_char = list("[email protected]%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`‘. ")length = len(ascii_char)img = Image.open(‘03.jpg‘) #讀取影像檔(width,height) = img.sizeimg = img.resize((int(width*0.9),int(height*0.5))) #對映像進行一定縮小print(img.size)def convert(img): img = img.convert("L") # 轉為灰階映像 txt = "" for i in range(img.size[1]): for j in range(img.size[0]): gray = img.getpixel((j, i)) # 擷取每個座標像素點的灰階 unit = 256.0 / length txt += ascii_char[int(gray / unit)] #擷取對應座標的字元值 txt += ‘\n‘ return txtdef convert1(img): txt = "" for i in range(img.size[1]): for j in range(img.size[0]): r,g,b = img.getpixel((j, i)) #擷取每個座標像素點的rgb值 gray = int(r * 0.299 + g * 0.587 + b * 0.114) #通過灰階轉換公式擷取灰階 unit = (256.0+1)/length txt += ascii_char[int(gray / unit)] # 擷取對應座標的字元值 txt += ‘\n‘ return txttxt = convert(img)f = open("03_convert.txt","w")f.write(txt) #儲存到檔案中f.close()
給圖片加上文字(福利預警,前方有福利!!!!)
#coding:utf-8from PIL import Image,ImageDraw,ImageFont#http://font.chinaz.com/zhongwenziti.html 字型下載網站img = Image.open(‘PDD01.jpg‘)draw = ImageDraw.Draw(img)myfont = ImageFont.truetype(‘HYLiuZiHeiJ.ttf‘,size=80)fillcolor = ‘pink‘(width, height) = img.size#第一個參數是加入字型的座標#第二個參數是文字內容#第三個參數是字型格式#第四個參數是字型顏色draw.text((40,100),u‘萌萌噠‘,font=myfont,fill=fillcolor)img.save(‘modfiy_pdd01.jpg‘,‘jpeg‘)
給圖片加上數字
這個大家應該見過的,就是有些頭像的左上方的那個小紅圈加上白色的數字,其實方法和上面那個加文字的差不多
講道理,我還不如用ps,移座標移到要死要死的
#coding:utf-8from PIL import Image,ImageDraw,ImageFontimg = Image.open("03.jpg")draw = ImageDraw.Draw(img)myfont = ImageFont.truetype(u"時光體.ttf",50)(width,height) = img.sizedraw.ellipse((width-40,0,width,40),fill="red",outline="red") #在圖上畫一個圓draw.text((width-30,-8),‘1‘,font=myfont,fill=‘white‘)img.save(‘03_modify.jpg‘)
產生4位隨機驗證碼
#coding:utf-8from PIL import Image,ImageDraw,ImageFont,ImageFilterimport random"""建立四位元的驗證碼"""#產生隨機驗證碼內容def rndTxt(): txt = [] txt.append(random.randint(97,123)) #大寫字母 txt.append(random.randint(65,90)) #小寫字母 txt.append(random.randint(48,57)) #數字 return chr(txt[random.randint(0,2)])#隨機顏色(背景)def rndColor1(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))#隨機顏色(字型)def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))#240x60:width = 60*4height = 60img = Image.new(‘RGB‘,(width,height),(255,255,255))font = ImageFont.truetype(u‘時光體.ttf‘,36)draw = ImageDraw.Draw(img)#填充每個像素for x in range(width): for y in range(height): draw.point((x,y),fill=rndColor1())#輸出文字for txt in range(4): draw.text((60*txt+10,10),rndTxt(),font=font,fill=rndColor2())#模糊化處理#img = img.filter(ImageFilter.BLUR)img.save("code.jpg")
python影像處理模組Pillow的學習