如何利用python给图片添加半透明水印
作者:北风忆夕 时间:2022-08-16 09:34:28
前言
本文主要给大家介绍了关于python图片添加半透明水印的相关资料,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧
示例代码:
# coding:utf-8
from PIL import Image, ImageDraw, ImageFont
def add_text_to_image(image, text):
font = ImageFont.truetype('C:\Windows\Fonts\STXINGKA.TTF', 36)
# 添加背景
new_img = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (0, 0, 0, 0))
new_img.paste(image, image.size)
# 添加水印
font_len = len(text)
rgba_image = new_img.convert('RGBA')
text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(text_overlay)
for i in range(0, rgba_image.size[0], font_len*40+100):
for j in range(0, rgba_image.size[1], 200):
image_draw.text((i, j), text, font=font, fill=(0, 0, 0, 50))
text_overlay = text_overlay.rotate(-45)
image_with_text = Image.alpha_composite(rgba_image, text_overlay)
# 裁切图片
image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
return image_with_text
if __name__ == '__main__':
img = Image.open("test.jpg")
im_after = add_text_to_image(img, u'测试使用')
im_after.save(u'测试使用.png')
效果图:
来源:https://www.cnblogs.com/l471151015/p/10345864.html
标签:python,半透明,水印
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 两个一样的字符串用==结果为false问题的解决
2023-01-24 08:30:59
python 19个值得学习的编程技巧
2021-09-21 22:42:30
python通过百度地图API获取某地址的经纬度详解
2021-11-06 12:14:45
![](https://img.aspxhome.com/file/2023/6/66456_0s.png)
HTTP中header头部信息详解
2023-06-11 23:33:17
修改Linux下MySQL 5.0的默认连接数
2009-09-01 10:16:00
python进行OpenCV实战之画图(直线、矩形、圆形)
2022-05-30 06:36:05
![](https://img.aspxhome.com/file/2023/9/71569_0s.jpg)
PHP Session变量不能传送到下一页的解决方法
2023-11-15 01:41:29
显示ASP页面源码的代码
2008-10-12 13:05:00
浅谈SQL Server中统计对于查询的影响分析
2012-06-06 20:08:23
python2爬取百度贴吧指定关键字和图片代码实例
2022-10-12 09:03:44
通过自学python能找到工作吗
2021-07-24 04:26:33
ORACLE11g随RHEL5系统自动启动与关闭的设置方法
2009-08-31 12:43:00
一劳永逸彻底解决pip install慢的办法
2023-08-10 20:30:52
opencv python 图像轮廓/检测轮廓/绘制轮廓的方法
2022-08-13 06:54:59
![](https://img.aspxhome.com/file/2023/4/77864_0s.png)
python中实现栈的三种方法
2023-02-10 18:17:14
简约的实现——服装网站设计
2009-03-06 12:43:00
![](https://img.aspxhome.com/file/UploadPic/20093/6/01-69s.jpg)
如何写JS数组sort的比较函数
2010-07-02 12:12:00
SQL Server 2005 内置工具建审查系统
2009-01-19 14:24:00
如何把图片也存到数据库中去?
2009-11-06 13:56:00
Go语言实现彩色输出示例详解
2023-09-14 01:19:45
![](https://img.aspxhome.com/file/2023/4/99634_0s.png)