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

Zahara ·
更新时间:2024-09-21
· 743 次阅读

图片上画线条

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:\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, 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()

以上这篇python 实现PIL模块在图片画线写字就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。

您可能感兴趣的文章:python处理图片之PIL模块简单使用方法Python实现图片添加文字python使用PIL给图片添加文字生成海报示例Python图片处理模块PIL操作方法(pillow)



画线 图片 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号