• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python 实现PIL模块在图片画线写字

python 搞代码 4年前 (2022-01-08) 16次浏览 已收录 0个评论

这篇文章主要介绍了python 实现PIL模块在图片画线写字,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

图片上画线条

 import sys from PIL import Image,ImageDraw im = Image.open("th.png-600") 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-600').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:\Windows\Fonts拷贝一份字体文件 当前脚本路径下 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, fi<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码*网</strong>ll=(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-600",img)#保存图片 fp.close()

以上就是python 实现PIL模块在图片画线写字的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python 实现PIL模块在图片画线写字
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址