【问题标题】:Python: how to convert an image in memory?Python:如何转换内存中的图像?
【发布时间】:2021-10-16 00:22:03
【问题描述】:
我在内存中有一张图片(从在线资源下载),我想在将其发送到其他在线位置之前将其转换为其他格式。
转换是 .webp 到 .jpg 但这并不真正相关。
使用 Pillow,我可以轻松地转换本地图像并将它们保存回磁盘,但我无法让它处理内存中的图像。
我不一定需要使用 Pillow。无需将任何内容保存到光盘即可转换图像的任何方式都可以。
【问题讨论】:
标签:
python
python-imaging-library
image-conversion
【解决方案1】:
我是使用 PIL 的 BytesIO 的新手,所以只需检查我的代码尝试,我的测试图像可以正常工作,请告诉我
from PIL import Image
from io import BytesIO
img = Image.open('test.webp')
print('image : ', img.format)
img.show()
# Write PIL Image to in-memory PNG
membuf = BytesIO()
img.save(membuf, format="png")
img = Image.open(membuf)
print('image : ', img.format)
img.show()