使用WSGI创建动态图像,不涉及任何文件 [英] Creating dynamic images with WSGI, no files involved

查看:232
本文介绍了使用WSGI创建动态图像,不涉及任何文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将动态创建的图像发送给我的用户,例如图表,图形等。这些图像是丢弃图像,它们只会被发送给一个用户然后被销毁,因此没有涉及文件 。

I would like to send dynamically created images to my users, such as charts, graphs etc. These images are "throw-away" images, they will be only sent to one user and then destroyed, hence the "no files involved".

我想将图像直接发送给用户,而不是先将其保存在文件系统中。使用PHP,可以通过将HTML文件中的图像链接到PHP脚本来实现,例如:

I would like to send the image directly to the user, without saving it on the file system first. With PHP this could be achieved by linking an image in your HTML files to a PHP script such as:

编辑:SO吞下我的图像标记:

edit: SO swallowed my image tag:

<img src="someScript.php?param1=xyz">

然后脚本将正确的标题(filetype => jpeg等)发送到浏览器并直接写入将图像重新保存到客户端,而不是暂时将其保存到文件系统。

The script then sent the correct headers (filetype=>jpeg etc) to the browser and directly wrote the image back to the client, without temporarily saving it to the file system.

我如何使用WSGI应用程序执行此类操作。目前我正在使用Python的内部SimpleWSGI服务器。我知道这个服务器主要用于演示目的而不是实际使用,因为它缺乏多线程功能,所以请不要指出这一点,我知道这一点,现在它满足了我的要求: )

How could I do something like this with a WSGI application. Currently I am using Python's internal SimpleWSGI Server. I am aware that this server was mainly meant for demonstration purposes and not for actual use, as it lacks multi threading capabilities, so please don't point this out to me, I am aware of that, and for now it fulfills my requirements :)

是否真的像将URL放入图像标签并使用WSGI处理请求一样简单,还是有更好的做法?

Is it really as simple as putting the URL into the image tags and handling the request with WSGI, or is there a better practise?

有没有人有这方面的经验,可以给我一些指示(请不要32位)

Has anyone had any experience with this and could give me a few pointers (no 32Bit ones please)

谢谢,

Tom

推荐答案

它与WSGI或php或任何其他特定网络技术无关。考虑

It is not related to WSGI or php or any other specific web technology. consider

<img src="someScript.php?param1=xyz">

一般来说,url someScript.php?param1 = xyz 服务器应该返回图像类型的数据,它会起作用

in general for url someScript.php?param1=xyz server should return data of image type and it would work

考虑这个例子:

from wsgiref.simple_server import make_server

def serveImage(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'image/png')]
    start_response(status, headers)

    return open("about.png", "rb").read()

httpd = make_server('', 8000, serveImage)
httpd.serve_forever()

这里任何指向serveImage的url都会返回一个有效的图像,你可以在任何 img 标签或任何其他可以使用图像的标签处使用它,例如css或背景图像

here any url pointing to serveImage will return a valid image and you can use it in any img tag or any other tag place where a image can be used e.g. css or background images

图像数据可以使用许多第三方库动态生成,例如PIL等
例如,请参阅使用python映像库动态生成图像的示例
http ://lost-theory.org/python/dynamicimg.html

Image data can be generated on the fly using many third party libraries e.g. PIL etc e.g see examples of generating images dynamically using python imaging library http://lost-theory.org/python/dynamicimg.html

这篇关于使用WSGI创建动态图像,不涉及任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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