简体   繁体   English

在Python / Web中提供动态图像的正确方法。使用PIL

[英]Correct way to serve a dynamic image in Python/Web.Py using PIL

I need to have a Python file act as if it's an image. 我需要有一个Python文件,就像它是一个图像。 ie The path to the Python file will be used inside an tag of a web page. 即Python文件的路径将用于网页的标记内。 The Python file will dynamically create an image using PIL (no intermediate save to file). Python文件将使用PIL动态创建图像(无中间保存到文件)。

I'm using Python 2.7, Web.Py, & PIL, and developing on 32-Bit Windows XP, using WAMPSS web server, and using the latest Chrome. 我正在使用Python 2.7,Web.Py和PIL,并在32位Windows XP上开发,使用WAMPSS Web服务器,并使用最新的Chrome。

I'm almost there. 我快到 I think. 我认为。

Here's a cut down example of my python image file: 这是我的python图像文件的缩减示例:

#!/home/bin/python
# #############################################################################
import web, sys
from PIL import Image, ImageDraw, ImageFont
# #############################################################################
class debug:
    def GET(self):
        # drawing with PIL (Python Image Library)
        # create a new 256x256 pixel image surface (default is black bg)
        img = Image.new("RGB", (256, 256))
        # set up the new image surface for drawing
        draw = ImageDraw.Draw (img)
        # REMOVED - Image manipulation
        # Output the image to the browser
        sys.stdout.write('Status: 200 OK\r\n')
        sys.stdout.write('Content-type: image/png\r\n')
        sys.stdout.write('\r\n')
        img.save(sys.stdout, "PNG")
# #############################################################################
urls = ( '/debug.py', 'debug' )
app = web.application(urls, globals())
if __name__ == "__main__":
    app.run()
# #############################################################################

Now, this works... Kinda. 现在,这有效......有点儿。

If I view this file directly in a browser (local AMPSS server), it shows the image. 如果我直接在浏览器(本地AMPSS服务器)中查看此文件,则会显示该图像。 But, it takes a long time to display/server (5+ seconds) compared to a similar 1k PNG image. 但是,与类似的1k PNG图像相比,显示/服务器(5秒以上)需要很长时间。 And Chrome console says it failed (although I can see the image) Chrome控制台说失败了(虽然我可以看到图片)

I think the browser is expecting something else that I'm not sending? 我认为浏览器期待其他我不发送的内容?

The real problem materialized when I use this image in JQuery and the OnLoad event. 当我在JQuery和OnLoad事件中使用此图像时,实际问题得以实现。 Things then became ...strange... for want of a better word. 事情变得......奇怪......因为缺少一个更好的词。 I appear to have events (not mine) fire that clear the image many seconds after it has been loaded & displayed. 我似乎有事件(不是我的)火,在加载和显示图像很多秒后清除图像。

The Chrome Console says that the GET fails when retrieving the image (although it does display the image, at least until something in JS makes it vanish again!) Chrome控制台说在检索图像时GET失败了(虽然它确实显示了图像,至少在JS中的某些东西再次消失之前!)

Interestingly, this issue doesn't seem to appear in latest Firefox. 有趣的是,这个问题似乎没有出现在最新的Firefox中。 The loading time is exaggerated in the latest Opera, but the JS doesn't glitch the image. 加载时间在最新的Opera中被夸大了,但JS并没有出现故障。 The same in IE8 在IE8中也一样

To confuse things even more, I have two Windows XP (32-bit) dev machines. 为了让事情更加混乱,我有两台Windows XP(32位)开发机器。 The JS glitching doesn't happen on one of the them. JS故障不会发生在其中一个上面。 And Chrome Console doesn't list any faults! Chrome控制台不会列出任何错误!

I've put a demo on the image file online here: LINKY And my in development page that uses this file here: LINKY The Jquery/JS will activate if you click around the white image-holders. 我在这里在线图像文件上放了一个演示: LINKY和我在这里使用这个文件的开发页面: LINKY如果点击白色图像持有者,Jquery / JS会激活。

I have no idea if this will work on your setup or not. 我不知道这是否适用于您的设置。 I'm just linking here for completeness. 我只是为了完整而链接到这里。

Any ideas on what I'm missing? 关于我缺少的任何想法?

In web.py you should return contents that have to be sent to the browser. 在web.py中,您应该返回必须发送到浏览器的内容。 Also use web.header to set HTTP header. 还可以使用web.header设置HTTP标头。

Save image to StringIO and return its contents: 将图像保存到StringIO并返回其内容:

import StringIO

# in GET(self):
web.header('Content-type', 'image/png')
buf = StringIO.StringIO()
img.save(buf, "PNG")
contents = buf.getvalue()
return contents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM