Python往图片写入文字,插入图片的方法

Python往图片写入文字,插入图片的方法

  • 使用情景
  • 效果
  • 代码

使用情景

做短视频编辑的时候,经常需要向图片中写入一些文字,插入图片,以增加图片的美观。本文将介绍使用python的Pillow库,将文字、图片插入图片之中。

效果

Python往图片写入文字,插入图片的方法_第1张图片

代码

本代码的功能,是将某个文件夹中的所有图片,合成为一个视频。

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)

你可能感兴趣的:(python,图片处理,python)