保存来自 io.BytesIO 的图像

Save image from io.BytesIO

我正在尝试将 Flask 中的一些代码转换为正常的终端可执行 python 代码。但是我遇到了一段代码,它试图将 return 图像作为对 API 调用的响应,我想存储该图像。

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

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

如何重新编写这部分以便将其保存为 'image.jpg'

扔掉所有代码,只使用:

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

我必须为 Mark Setchell 做出贡献: 我想保存带有质量设置的 jpg,只需使用:

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