歡迎您光臨本站 註冊首頁

python 實現PIL模塊在圖片畫線寫字

←手機掃碼閱讀     retouched @ 2020-06-01 , reply:0

圖片上畫線條

 import sys from PIL import Image,ImageDraw im = Image.open("th.png") draw = ImageDraw.Draw(im) #實例化一個對象 draw.line((0, 0) + im.size, fill=128, width=5) #線的起點和終點,線寬 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 image base = Image.open('th.jpg').convert('RGBA') # make a blank image for the text, initialized to transparent text color txt = Image.new('RGBA', base.size, (255,255,255,0)) # get a font 需要在C:WindowsFonts拷貝一份字體文件 當前腳本路徑下 fnt = ImageFont.truetype('cambriai.ttf', 40) # get a drawing context d = ImageDraw.Draw(txt) # draw text, half opacity d.text((10,10), "Hello", fOnt=fnt, fill=(255,255,255,128)) # draw text, full opacity d.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

補充知識:python對圖像中的人臉進行畫框(人臉的位置數據記錄在記事本文件中)

我就廢話不多說了,大家還是直接看代碼吧!

 import numpy as py import os import cv2 as cv with open('labelFaceData.txt','r')as fp:#打開記錄了數據的記事本文件 pictureNumber = 0#用來記錄照片的數量 while 1: count = 1 line = fp.readline()#讀取文件中每一行的數據 if not line:#如果讀取失敗則退出 break pictureNumber+=1#圖片數加1 str1 = line.split()#用一個數組以字符串的形式儲存文件中的數據 img = cv.inread(str[0])#str[0]中存放的是要讀取的圖片地址,用cv.inread讀取它 faceNumber = (len(str1)-1)/16#用來記錄人臉的總數 for i in reage(faceNumber):#用for循環對人臉進行畫框 x = int(str1[count+1])#x,y,w,h為畫框需要的點 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對圖像進行畫框 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()

[retouched ] python 實現PIL模塊在圖片畫線寫字已經有383次圍觀

http://coctec.com/docs/python/shhow-post-236493.html