简体   繁体   English

从 io.BytesIO 保存图像

[英]Save image from io.BytesIO

I am trying to convert some code in flask to normal terminal executable python code.我正在尝试将 flask 中的一些代码转换为普通终端可执行 python 代码。 But I have come across a certain section of code that tries to return an image as the response to that API call and I would like to store that image.但是我遇到了一段代码,它试图返回一个图像作为对该 API 调用的响应,我想存储该图像。

_, buffer = cv2.imencode('.jpg', img2)

return send_file(
             io.BytesIO(buffer),
             mimetype='image/jpeg',
             as_attachment=True,
             attachment_filename='image.jpg')

How do I re write this part so that I can save it as 'image.jpg'如何重新编写这部分以便我可以将其保存为“image.jpg”

Throw all the code away and just use:扔掉所有代码,然后使用:

cv2.imwrite('image.jpg', img2)

I have to contribute to Mark Setchell: I you want to save jpg with quality setting, just use:我必须为 Mark Setchell 做出贡献:我想用质量设置保存 jpg,只需使用:

cv2.imwrite('image.jpg', img2, [int(cv2.IMWRITE_JPEG_QUALITY), 100])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM