Python 官方文档:入门教程 => 点击学习
目录前言一、PIL是什么?二、安装PIL三、查看PIL版本四、使用PIL库给图片添加文本水印1.引入库2.打开图片文件3.新建一个Draw对象4.设置水印文字、字体、大小5.设置水印
大家好,本篇给大家分享一下通过python的PIL库给图片添加文本水印。
PIL是Python Imaging Library的缩写,它是Python语言中常用的图像处理库之一。它提供了丰富的图像处理功能,包括打开、保存、裁剪、旋转、缩放等操作,并支持多种图像格式。
pip install pillow
pip show pillow
Name: Pillow
Version: 9.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark@python-pillow.org
License: HPND
Requires:
Required-by: image, imageio, matplotlib, pytesseract, Wordcloud
from PIL import Image, ImageDraw, ImageFont
local = '/Users/kkstar/Downloads/video/pic/'
image = Image.open(local+"demo.jpg")
draw = ImageDraw.Draw(image)
text = '@空空star'
font = ImageFont.truetype('STHeitiMedium.ttc', size=80)
# 通过名称设置颜色-黄色
color = 'yellow'
# 通过RGB值设置颜色-红色
color = (255, 0, 0)
# 通过RGBA值设置颜色-白色
color = (255,255,255,0)
# 通过十六进制设置颜色-绿色
color = '#6FE000'
text_width, text_height = draw.textsize(text, font)
x = 30
y = 30
x = image.width-text_width-30
y = image.height-text_height-30
其他位置调整x、y的值即可。这个30是我这样设置的,你也可以根据自己的喜好来调整。
draw.text((x, y), text, font=font, fill=color)
image.save(local+'image_with_watermark.jpg')
到此这篇关于使用Python中PIL库给图片添加文本水印的文章就介绍到这了,更多相关Python PIL库添加文本水印内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 使用Python中PIL库给图片添加文本水印
本文链接: https://www.lsjlt.com/news/210383.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0