Flask响应内容为图片时怎么体现,,比如请求 http://


比如请求 http://xxx.com/image?name=filename&size=128 则返回相应大小的图片

返回的是一张图片,128时返回filename_128.png,256时返回filename_256.png
这个该怎么写呢?

用 tornado 写过一个
流程大概就是取参数去读本地图片, 再缩放尺寸吐出去

import osimport magicimport StringIOfrom PIL import Imagefrom PIL.ExifTags import TAGSimport tornado.webclass img(tornado.web.RequestHandler, tornado.web.StaticFileHandler):  def get_mime(self, path):    mime = magic.Magic(mime=True)    return mime.from_file(path)  def send_img(self, ret, mime_type):    self.set_header("Content-Type", mime_type)    self.set_header('Content-Length', len(ret))    self.finish(ret)  def resize(self, path, size):    mime_type = self.get_mime(path)    if not mime_type or not os.path.exists(path) or not os.path.isfile(path):      raise tornado.web.HTTPError(404)    img = Image.open(path)    _size = tuple([int(s) for s in size.split('x')])    img.thumbnail(_size, Image.ANTIALIAS)    buffer = StringIO.StringIO()    img.save(buffer, mime_type[6:])    ret = buffer.getvalue()    self.send_img(ret, mime_type)  def get(self, path, includ_body=True):    path = self.parse_url_path(path)    abspath = os.path.abspath(os.path.join(self.root, path))    size = self.get_argument('s', None)    if size:      self.resize(abspath, size)    else:      tornado.web.StaticFileHandler.get(self, path, includ_body)

用IOstring和make_response生成图片响应,搜一下有一篇flask生成验证码的博客文章,你可以参考下

编橙之家文章,

评论关闭