做短视频编辑的时候,经常需要向图片中写入一些文字,插入图片,以增加图片的美观。本文将介绍使用python的Pillow库,将文字、图片插入图片之中。
本代码的功能,是将某个文件夹中的所有图片,合成为一个视频。
from PIL import ImageFont, ImageDraw, Image
def add_text_on_image():
img1 = Image.open("example4.jpg")
draw = ImageDraw.Draw(img1)
fnt = ImageFont.truetype(r'C:\Windows\Fonts\STKAITI.TTF', 100)
text = "人生什么时候"
draw.text((100, 120), text, fill='blue', font=fnt)
text = "都不晚"
draw.text((100, 240), text, fill='blue', font=fnt)
img1.save("output1.jpg")
return img1
def add_image_on_image(img1):
img2 = Image.open("emoji.gif")
img1.paste(img2, (300, 400)) # 纵向拼接
img1.save("output2.jpg")
if __name__ == '__main__':
img1 = add_text_on_image()
add_image_on_image(img1)