从内存中读取图片
例如 jpgdata
是 open()
函数打开的文件直接通过 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 usefrom 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()