我正在尝试在 GAE 上的图像上覆盖一些文本。现在他们公开了 PIL 库,这应该不是问题。
这就是我所拥有的。它有效,但我不禁认为我应该直接写入背景图像,而不是创建单独的叠加图像然后合并。
我可以使用Image.frombuffer什么的吗,我已经尝试过了,但我就是不明白...
from PIL import Image, ImageDraw, ImageFont
from google.appengine.api import images
from google.appengine.ext import blobstore
from google.appengine.api import files
def compose(key):
# create new image
text_img = Image.new('RGBA', (800,600), (0, 0, 0, 0))
draw = ImageDraw.Draw(text_img)
draw.text((0, 0), 'HELLO TEXT', font=ImageFont.load_default())
# no write access on GAE
output = StringIO.StringIO()
text_img.save(output, format="png")
text_layer = output.getvalue()
output.close()
# read background image
blob_reader = blobstore.BlobReader(key)
background = images.Image(blob_reader.read())
# merge
merged = images.composite([(background, 0, 0, 1.0, images.TOP_LEFT),
(text_layer, 0, 0, 1.0, images.TOP_LEFT)],
800, 600)
# save
file_name = files.blobstore.create(mime_type='image/png')
with files.open(file_name, 'a') as f:
f.write(merged)
files.finalize(file_name)