0

有没有最流行的方式在 Python 中的图像上添加文本?我发现了一些完全不同的方法,这似乎是最好的,但它不起作用:

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 
img = Image.open("/full_path/1.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 16)
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
img.save('/full_path/sample-out.jpg')

运行后,图片上仍然没有文字。

4

1 回答 1

2

尝试这个:

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

img=Image.open("pathToImage")

font = ImageFont.truetype("pathToFont",10)

draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
于 2013-08-18T03:44:47.693 回答