python 将5张图像绘制到一张图上,并在绘制的图的下方显示一行文字
时间: 2023-04-12 14:01:19 浏览: 119
可以使用Pillow库来实现这个功能。以下是一个示例代码:
```python
from PIL import Image, ImageDraw, ImageFont
# 打开5张图片
img1 = Image.open("image1.jpg")
img2 = Image.open("image2.jpg")
img3 = Image.open("image3.jpg")
img4 = Image.open("image4.jpg")
img5 = Image.open("image5.jpg")
# 获取图片的宽度和高度
width, height = img1.size
# 创建一个新的图像,宽度为5张图片的宽度,高度为5张图片的高度加上一行文字的高度
new_img = Image.new("RGB", (width, height*5+50), (255, 255, 255))
# 在新图像上绘制5张图片
new_img.paste(img1, (0, 0))
new_img.paste(img2, (0, height))
new_img.paste(img3, (0, height*2))
new_img.paste(img4, (0, height*3))
new_img.paste(img5, (0, height*4))
# 在新图像下方绘制一行文字
draw = ImageDraw.Draw(new_img)
font = ImageFont.truetype("arial.ttf", 20)
text = "这是一行文字"
text_width, text_height = draw.textsize(text, font)
draw.text(((width-text_width)/2, height*5+10), text, font=font, fill=(0, 0, 0))
# 保存新图像
new_img.save("result.jpg")
```
这个代码会将5张图片绘制到一张图上,并在绘制的图的下方显示一行文字。你可以将图片和字体替换成你自己的,也可以调整文字的位置和大小。
阅读全文
相关推荐


















