国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python 實(shí)現(xiàn)PIL模塊在圖片畫線寫字

瀏覽:5日期:2022-07-25 11:32:00

圖片上畫線條

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)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 禄丰县| 雷山县| 崇仁县| 淄博市| 渝北区| 永登县| 泗洪县| 桃江县| 上饶市| 太白县| 内丘县| 莒南县| 元江| 天柱县| 航空| 湄潭县| 宜都市| 宜良县| 保山市| 武平县| 巴东县| 阿瓦提县| 六枝特区| 淮南市| 盐边县| 廉江市| 鄂托克前旗| 夹江县| 太原市| 攀枝花市| 宣武区| 独山县| 体育| 前郭尔| 宁安市| 海阳市| 南昌县| 汕头市| 眉山市| 临安市| 滨州市|