推荐学习书目
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 · Dec 9, 2019 · 4592 views
    This topic created in 2422 days ago, the information mentioned may be changed or developed.

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

    4 replies    2019-12-09 18:16:08 +08:00
    Trim21
        1
    Trim21  
       Dec 9, 2019 via Android   ❤️ 1
    用 ByteIO
    Latin
        2
    Latin  
       Dec 9, 2019   ❤️ 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  
       Dec 9, 2019
    上次在这里讨论过
    https://www.v2ex.com/t/619626
    XGF
        4
    XGF  
       Dec 9, 2019
    flask 的 Response 或者 make_response 都行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3206 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:01 · PVG 21:01 · LAX 06:01 · JFK 09:01
    ♥ Do have faith in what you're doing.