App Engine,PIL和覆盖文本 [英] App Engine, PIL and overlaying text

查看:197
本文介绍了App Engine,PIL和覆盖文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在GAE上的图像上叠加一些文字。现在他们暴露了PIL库它不应该是问题。

I'm trying to overlay some text over an image on GAE. Now they expose the PIL library it should not be problem.

这就是我所拥有的。它有效,但我不禁想到我应该直接写入背景图像,而不是创建一个单独的叠加图像然后合并。

Here's what I have. It works, but I can't help think I should be writing directly to the background image rather than creating a separate overlay image and then merging.

我可以使用 Image.frombuffer 或其他什么,我已经给了它一个但是我是只是没有得到它...

Can I use Image.frombuffer or something, I've given it a go but I'm just not getting it...

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)


推荐答案

您应该使用 [Image.open] [1] 方法。 Image.frombuffer Image.fromstring 解码像素数据而不是原始图像。

You should use the [Image.open][1] method instead. Image.frombuffer and Image.fromstring decode pixel data not raw images.

在你的情况下,你可以使用类似的东西:

In your case you could use something like:

blob_reader = blobstore.BlobReader(key)
text_img = Image.open(blob_reader)
.........

这篇关于App Engine,PIL和覆盖文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆