from PIL import Image, ImageDraw, ImageFont
import os
def water_work(img):
x, y = img.size
textx = int(x*1.5)
texty = int(y*1.5)
blank = Image.new('RGB', (textx, texty), 'white')
draw = ImageDraw.Draw(blank)
font = ImageFont.truetype('simsun.ttc', size=50)
text = '王小涛_同學'
draw.ink = 100
draw.text(((textx-x/2)/2, (texty)/2), text, font=font)
One_blank = blank.rotate(30)
box = (730, 610, 1000, 800)
textImage = One_blank.crop(box)<img src="https://img-blog.csdn.net/20150208171613764" alt="" />
pastebox = (int(x/2 + 80), int(y/2))
blank2 = Image.new('RGB', (x, y), 'white')
blank2.paste(textImage, pastebox)
waterImage = Image.blend(img, blank2, 0.2) #图片合并<img src="https://img-blog.csdn.net/20150208171708598" alt="" />
waterImage.save(image, quality=100)
for img in os.listdir('C:/Users/hengli/Pictures/lovewallpaper'):
if img.endswith('.jpg'):
image = os.path.join('C:/Users/hengli/Pictures/lovewallpaper/', img)
img = Image.open(image)
water_work(img)
Python3 照片打水印
最新推荐文章于 2024-11-05 15:28:12 发布
使用Python的PIL库,通过定义函数water_work()实现为图片添加水印。首先创建空白图片,写入文字,旋转并裁剪水印,然后将其与原图融合并保存。该代码可以批量处理指定文件夹内的.jpg图片。
摘要由CSDN通过智能技术生成