V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
miniyao
V2EX  ›  Python

Flask 可以 send file 直接(response)返回页面一张图片吗?(不在本地磁盘上保存一次)

  •  
  •   miniyao · 2019-12-09 12:26:30 +08:00 · 3981 次点击
    这是一个创建于 1572 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用户点击之后,PIL image 生成一张图片返回给页面。但是这个图片不想在本地保存 /删除一次,直接用二进制的数据流完成,可以实现吗?

    4 条回复    2019-12-09 18:16:08 +08:00
    Trim21
        1
    Trim21  
       2019-12-09 12:28:25 +08:00 via Android   ❤️ 1
    用 ByteIO
    Latin
        2
    Latin  
       2019-12-09 15:46:42 +08:00   ❤️ 1
    @app.route("/image")
    def gen():
    img = Image.new("RGB", (350, 40), (123, 222, 222)) # 颜色模式,长宽,rgb 里面的颜色
    f = BytesIO()
    img.save(f, "png")
    resp = make_response(f.getvalue())
    resp.headers["Content-Type"] = "image/jpeg"
    return resp
    superrichman
        3
    superrichman  
       2019-12-09 16:34:55 +08:00
    上次在这里讨论过
    https://www.v2ex.com/t/619626
    XGF
        4
    XGF  
       2019-12-09 18:16:08 +08:00
    flask 的 Response 或者 make_response 都行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2507 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:44 · PVG 23:44 · LAX 08:44 · JFK 11:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.