python 實(shí)現(xiàn)PIL模塊在圖片畫線寫字
圖片上畫線條
import sysfrom PIL import Image,ImageDrawim = Image.open('th.png')draw = ImageDraw.Draw(im) #實(shí)例化一個(gè)對(duì)象draw.line((0, 0) + im.size, fill=128, width=5) #線的起點(diǎn)和終點(diǎn),線寬draw.line((0, im.size[1], im.size[0], 0), fill=128)draw.line((0,im.size[1]/2)+(im.size[0]/2,im.size[1]), fill=128, width=5)im.show()
圖片上寫字
from PIL import Image, ImageDraw, ImageFont# get an imagebase = Image.open(’th.jpg’).convert(’RGBA’)# make a blank image for the text, initialized to transparent text colortxt = Image.new(’RGBA’, base.size, (255,255,255,0))# get a font 需要在C:WindowsFonts拷貝一份字體文件 當(dāng)前腳本路徑下fnt = ImageFont.truetype(’cambriai.ttf’, 40)# get a drawing contextd = ImageDraw.Draw(txt)# draw text, half opacityd.text((10,10), 'Hello', font=fnt, fill=(255,255,255,128))# draw text, full opacityd.text((10,60), 'World', font=fnt, fill=(255,255,255,255))fillcolor = '#ff0000' #字體顏色d.text((base.size[0]-20,10), '4', font=fnt, fill=fillcolor)out = Image.alpha_composite(base, txt)out.show()
參考官方文檔 https://pillow.readthedocs.io/en/stable/reference/Image.html
補(bǔ)充知識(shí):python對(duì)圖像中的人臉進(jìn)行畫框(人臉的位置數(shù)據(jù)記錄在記事本文件中)
我就廢話不多說(shuō)了,大家還是直接看代碼吧!
import numpy as pyimport osimport cv2 as cvwith open(’labelFaceData.txt’,’r’)as fp:#打開記錄了數(shù)據(jù)的記事本文件 pictureNumber = 0#用來(lái)記錄照片的數(shù)量 while 1: count = 1 line = fp.readline()#讀取文件中每一行的數(shù)據(jù) if not line:#如果讀取失敗則退出 break pictureNumber+=1#圖片數(shù)加1 str1 = line.split()#用一個(gè)數(shù)組以字符串的形式儲(chǔ)存文件中的數(shù)據(jù) img = cv.inread(str[0])#str[0]中存放的是要讀取的圖片地址,用cv.inread讀取它 faceNumber = (len(str1)-1)/16#用來(lái)記錄人臉的總數(shù) for i in reage(faceNumber):#用for循環(huán)對(duì)人臉進(jìn)行畫框 x = int(str1[count+1])#x,y,w,h為畫框需要的點(diǎn) y = int(str1[count+2]) w = int(str1[count+3]) h = int(str1[count+4]) cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),3,4,0)#用rectangle對(duì)圖像進(jìn)行畫框 count+=16 #cv.namedWindow(str[0],0) #cv.imshow(str[0],img); #cv.waitKey(0) cv.imwrite('./result/image1_'+str(pictureNumber)+'.jpg',img)#保存圖片fp.close()
以上這篇python 實(shí)現(xiàn)PIL模塊在圖片畫線寫字就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)2. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移3. XML解析錯(cuò)誤:未組織好 的解決辦法4. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式5. HTML5 Canvas繪制圖形從入門到精通6. XML入門的常見問題(四)7. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))8. JavaWeb Servlet中url-pattern的使用9. XML入門的常見問題(一)10. asp批量添加修改刪除操作示例代碼
