Flask响应内容为图片时怎么体现,,比如请求 http://
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生成验证码的博客文章,你可以参考下
编橙之家文章,
相关内容
- 想用Python去掉{通配符}正则表达式怎么写,python正则表
- Python音频格式转换问题m4a转amr,pythonm4a,如何实现m4a 转
- 请问Python web应用使用哪种目录结构来组织代码比较好,
- Python语言file和open区别是什么,python语言fileopen,Python里
- python解释器问题,错误提示:0x80070652怎么办?,python0x8
- 网站迁移问题,django静态html目录url如何访问,djangourl,处
- python字符串嵌套与变量之间的问题,python嵌套,有没有类
- Python如何捕获微信摄推送服务端事件思路,python服务端
- python运行环境哪种轻巧又实用,python运行环境,在本机开
- 求问如下python解析库工作原理是什么,python工作原理
评论关闭