python 图片上添加文字

搬运链接https://www.cnblogs.com/tk091/p/4331327.html

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

#设置字体,如果没有,也可以不设置
#font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",13)
font = ImageFont.truetype("C:\\Windows\\Fonts\\simsun.ttc",60)    #现在是宋体

#打开底版图片
imageFile = "e:\\1.jpg"
im1=Image.open(imageFile)

# 在图片上添加文字 1
draw = ImageDraw.Draw(im1)
#draw.text((0, 0),"1",(255,255,0),font=font)
#draw.text((500, 50),"1",(255,255,0))
draw.text((500, 50),"xie",(0,0,0),font=font)
draw = ImageDraw.Draw(im1)

# 保存
im1.save("e:\\target.png")
print('运行结束')

 2.改动版本Xdd

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

#设置字体,如果没有,也可以不设置
#font = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",13)
font = ImageFont.truetype("C:\\Windows\\Fonts\\simsun.ttc",60)    #现在是宋体

#打开底版图片,确定书写位置
imageFile = "e:\\1.jpg"
img=Image.open(imageFile)

#print(width,height)
#print(type(width))


# 在图片上添加文字 
str1 = 'xdd'
str2 = ''
width=img.width
height=img.height
position1 = (width/2,height/10)
position2 = (width/4,height/5)
color = (255,255,0)
draw = ImageDraw.Draw(img)
draw.text(position1,str1,color,font=font)
draw.text(position2,str2,color,font=font)
#draw = ImageDraw.Draw(img)

# 保存图片
img.save("e:\\1_xdd.jpg")
print('运行结束')

 

 

posted @ 2019-12-04 15:01  xdd1997  阅读(2798)  评论(0编辑  收藏  举报