PIL添加文字
时间: 2023-08-31 07:05:41 浏览: 40
PIL(Python Imaging Library)是一个Python图像处理库,可以用于图像处理、图像增强、图像格式转换等操作。下面是一个使用PIL在图片上添加文字的例子:
```python
from PIL import Image, ImageDraw, ImageFont
# 打开图片
image = Image.open('example.jpg')
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(image)
# 定义要添加的文字内容、字体、字号和颜色
text = 'Hello, World!'
font = ImageFont.truetype('arial.ttf', 36)
color = (255, 255, 255)
# 获取要添加的文字的宽度和高度
text_width, text_height = draw.textsize(text, font)
# 计算要添加的文字的位置
x = (image.width - text_width) / 2
y = (image.height - text_height) / 2
# 在图片上添加文字
draw.text((x, y), text, font=font, fill=color)
# 保存修改后的图片
image.save('example_with_text.jpg')
```
以上代码会在图片中央添加一段文字,并将修改后的图片保存为`example_with_text.jpg`。你可以根据需要修改文字内容、字体、字号、颜色、位置等参数。
相关问题
PIL添加文字背景
要添加文字背景,您可以使用PIL库中的ImageDraw模块。以下是一个简单的示例:
```python
from PIL import Image, ImageDraw, ImageFont
# 打开图像文件
image = Image.open('example.jpg')
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(image)
# 设置文本和字体
text = 'Hello, World!'
font = ImageFont.truetype('arial.ttf', size=36)
# 获取文本的宽度和高度
text_width, text_height = draw.textsize(text, font)
# 计算文本区域的位置和大小
x = (image.width - text_width) // 2
y = (image.height - text_height) // 2
w = text_width + 20
h = text_height + 20
# 绘制背景矩形
draw.rectangle((x, y, x+w, y+h), fill=(255, 255, 255))
# 绘制文本
draw.text((x+10, y+10), text, fill=(0, 0, 0), font=font)
# 保存图像文件
image.save('example_with_text.jpg')
```
在上面的示例中,我们首先打开一个图像文件,并创建一个ImageDraw对象。然后,我们设置要添加的文本和字体,并使用textsize()方法计算文本的大小。接下来,我们计算文本区域的位置和大小,并使用rectangle()方法绘制一个填充为白色的矩形。最后,我们使用text()方法在文本区域内绘制文本,并保存图像文件。
请注意,上面的示例仅是一个简单的示例,您可以根据需要进行更改和调整。
python添加文字代码
要在Python中添加文字,可以使用以下代码:
```python
# 导入所需库
from PIL import Image, ImageDraw, ImageFont
# 打开要添加文字的图片
img = Image.open('example.jpg')
# 创建一个可编辑的图片副本
draw = ImageDraw.Draw(img)
# 定义要添加的文字
text = 'Hello, World!'
# 定义要使用的字体和字号
font = ImageFont.truetype('arial.ttf', 36)
# 定义要添加文字的位置和颜色
pos = (50, 50)
color = (255, 255, 255)
# 在图片上添加文字
draw.text(pos, text, color, font=font)
# 保存修改后的图片
img.save('example_with_text.jpg')
```
这段代码使用了Pillow库中的Image、ImageDraw和ImageFont模块来打开、编辑和保存图片,并使用了arial.ttf字体和36号字号来添加文字。你可以根据需要修改这些参数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)