Creating dynamic images with WSGI, no files involved

后端 未结 4 1862

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 dest

4条回答
  •  萌比男神i
    2021-01-21 06:23

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

    
    

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

    Consider this example:

    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()
    

    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

    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

提交回复
热议问题