本文介绍了使用PIL在App Engine上绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读主题 。

下面的代码会显示一张破碎的图片,我应该如何改正这一点?

The code below will show a broken image, how should I correct that?

class TestImg(webapp2.RequestHandler):
    def get(self):
        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())
        self.response.headers['Content-Type'] = "image/png"
        self.response.write(draw)


推荐答案

基于Tim Hoffman的说法,你的课堂应该是这样的:

Building on what Tim Hoffman said, your class would look something like this:

import StringIO

class TestImg(webapp2.RequestHandler):
    def get(self):
        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())

        output = StringIO.StringIO()
        text_img.save(output, format="png")
        text_layer = output.getvalue()
        output.close()

        self.response.headers['Content-Type'] = 'image/png'
        self.response.write(text_layer)

这篇关于使用PIL在App Engine上绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:44