Python给图⽚加⽔印代码
import time
import os
try:
容桂邮编from PIL import Image, ImageDraw, ImageFont, ImageEnhance
except ImportError:
import Image, ImageDraw, ImageFont, ImageEnhance
fontpath = "/home/hey/f"waterfontpath = "/home/hey/f"out_file = "/home/hey/Desktop/img/"# def logo_watermark(img, logo_path):
# '''''
# 添加⼀个图⽚⽔印,原理就是合并图层,⽤png⽐较好
# '''
# baseim = img
# logoim = Image.open(logo_path)
# bw, bh = baseim.size
# lw, lh = logoim.size
# baseim.paste(logoim, (bw - lw, bh - lh))
# baseim.save('test3.jpg', 'JPEG')
# print(u'logo⽔印组合成功')
def text_watermark(img, text, out_file, angle=23, opacity=0.50):
'''''
添加⼀个⽂字⽔印,做成透明⽔印的模样,应该是png图层合并
www.pythoncentral.io/watermark-images-python-2x/
这⾥会产⽣著名的 ImportError("The _imagingft C module is not installed") 错误
Pillow通过安装来解决 pip install Pillow
'''
watermark = w('RGBA', img.size, (0, 0, 0,0)) # 我这⾥有⼀层⽩⾊的膜,去掉(255,255,255) 这个参数就好了
FONT = waterfontpath
size = 70
n_font = uetype(FONT, size) # 得到字体
n_width, n_height = size(text)
n_font = uetype(FONT, size=size)
灭蚊方法# watermark = size((text_width,text_height), Image.ANTIALIAS)
draw = ImageDraw.Draw(watermark, 'RGBA') # 在⽔印层加画笔
###左3
<((watermark.size[0] - 1100, watermark.size[1] - 1850),text, font=n_font, fill="#ccc")
<((watermark.size[0] - 1250, watermark.size[1] - 1400), text, font=n_font, fill="#ccc")
<((watermark.size[0] - 1400, watermark.size[1] - 950), text, font=n_font, fill="#ccc")
###右3
<((watermark.size[0] - 650, watermark.size[1] - 1600), text, font=n_font, fill="#ccc")
<((watermark.size[0] - 800, watermark.size[1] - 1150), text, font=n_font, fill="#ccc")
<((watermark.size[0] - 950, watermark.size[1] - 700), text, font=n_font, fill="#ccc")
watermark = ate(angle, Image.BICUBIC)
alpha = watermark.split()[3]
葡萄酒的做法alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
watermark.putalpha(alpha)
out_file = out_file + time.strftime("%Y%m%d%H%M%S") + ".jpg" Imageposite(watermark, img, watermark).save(out_file, 'JPEG')
print("⽂字⽔印成功")
return out_file
车辆剐蹭怎么走保险def image_to_text():
out_file_path = text_watermark(im, '三湘银⾏抵押快贷', out_file)
targetimg = Image.open(out_file_path)
# 将img添加到画板
imgdraw = ImageDraw.Draw(targetimg)
# 设置需要绘制的字体参数:字体名,字体⼤⼩
# 设置需要绘制的字体参数:字体名,字体⼤⼩
imgfont = uetype(fontpath, size=22)
# 字体颜⾊
fillcolor = "black" # 获取img的宽和⾼
# imgw,imgh = img.size
# 开始将⽂字内容绘制到img的画板上参数:坐标,绘制内容,填充颜⾊,字体((20, 20),"测试", fill=fillcolor, font=imgfont)
<((0, 0), "测试", fill=fillcolor, font=imgfont)
# # 设置img的保存路径和⽂件名
# imgsavetarget = savepath + time.strftime("%Y%m%d%H%M%S") + ".png"
# 开始保存
targetimg.save(out_file_path, "png")
# 返回保存结果
return out_file_path
if __name__ == "__main__":
# im = Image.open('/home/hey/Desktop/test.jpg') # image 对象
# logo_watermark(im, '/home/hey/Desktop/logo.jpg')
sourceimg1 = "/home/hey/Desktop/sx_1.jpg" im = Image.open(sourceimg1) # image 对象result = image_to_text()
sourceimg2 = "/home/hey/Desktop/sx_2.jpg" im = Image.open(sourceimg2) # image 对象result = image_to_text()谢师感恩词
恍然大悟意思sourceimg3 = "/home/hey/Desktop/sx_3.jpg" im = Image.open(sourceimg3) # image 对象result = image_to_text()
sourceimg4 = "/home/hey/Desktop/sx_4.jpg" im = Image.open(sourceimg4) # image 对象result = image_to_text()
print(result)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论