python批量出图像标题
时间: 2023-07-18 13:31:30 浏览: 43
你可以使用Python中的Pillow库来批量生成图像标题。以下是一个基本的代码示例:
```python
from PIL import Image, ImageDraw, ImageFont
import os
# 设置标题文本和字体
text = 'My Image Title'
font = ImageFont.truetype('arial.ttf', 20)
# 设置图像文件夹路径
folder_path = 'image_folder/'
# 遍历文件夹中的图像文件
for file_name in os.listdir(folder_path):
if file_name.endswith('.jpg') or file_name.endswith('.png'):
# 打开图像文件并添加标题
image = Image.open(os.path.join(folder_path, file_name))
draw = ImageDraw.Draw(image)
text_width, text_height = draw.textsize(text, font)
draw.text(((image.width - text_width) / 2, image.height - text_height - 10), text, font=font, fill='white')
# 保存带有标题的图像文件
image.save(os.path.join(folder_path, 'with_title_' + file_name))
```
这个代码示例假设你的图像文件都保存在一个名为“image_folder”的文件夹中,使用Arial字体,将标题添加到图像底部中心,并将带有标题的图像保存在原始文件名前加上“with_title_”的文件名中。你可以根据自己的需要进行修改。