Image 从内存中加载图片 写入内存

创建日期: 2022-11-26 21:36 | 作者: 风波 | 浏览次数: 12 | 分类: Python

从内存中读取图片

例如 jpgdataopen() 函数打开的文件直接通过 read() 函数读取的文件内容,那么可以通过下面的方式加载到 Image 对象中:

来源:https://stackoverflow.com/questions/8821259/python-imaging-load-jpeg-from-memory

PIL's Image.open object accepts any file-like object. That means you can wrap your Image data on a StringIO object, and pass it to Image.Open

from io import BytesIO
file_jpgdata = BytesIO(jpgdata)
dt = Image.open(file_jpgdata)

Or, try just passing self.rfile as an argument to Image.open - it might work just as well. (That is for Python 3 - for Python 2 use from cStringIO import StringIO as BytesIO)

写入到内存

来源:https://stackoverflow.com/questions/68760650/python-how-to-convert-an-image-in-memory

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()
12 浏览
8 爬虫
0 评论