首页 > 解决方案 > 如何使用python将文本放在多个图像上?

问题描述

from PIL import Image, ImageDraw, ImageFont
import glob
import os
images = glob.glob("directory_path/*.jpg")

for img in images:
    images = Image.open(img)
    draw = ImageDraw.Draw(images)
    font = ImageFont.load_default() #Downloaded Font from Google font
    text = "Text on all images from directory"
    draw.text((0,150),text,(250,250,250),font=font)
    images.save(img)

我必须在所有图像上添加文字,我已经尝试了上面的代码,但它不起作用

标签: pythonpython-imaging-library

解决方案


Here is the solution

from PIL import Image,ImageDraw,ImageFont
import glob
import os

images=glob.glob("path/*.jpg")

for img in images:
    images=Image.open(img)
    draw=ImageDraw.Draw(images)
    font=ImageFont.load_default()
    text="Whatever text"
    draw.text((0,240),text,(250,250,250),font=font)
    images.save(img)

推荐阅读