如何将字节数组转换为图像而不将其保存到python中的文件中?

时间:2019-05-29 12:30:25

标签: python-3.x

如何在不使用python文件的情况下将字节数组转换为图像?

#image to bytearray
with open("C:/Downloads/myimage.jpeg", "rb") as image:
    f = image.read()
    b = bytearray(f)
#bytearray to image
f = open('xyz.jpeg', 'wb')
f.write(bytearray(b))
f.close()

1 个答案:

答案 0 :(得分:0)

您可以使用Pillow。首先安装它:pip install Pillow

import io
from PIL import Image

#image to bytearray
with open("C:/Downloads/myimage.jpeg", "rb") as image:
    f = image.read()
    b = bytearray(f)

image = Image.open(io.BytesIO(b))
image.show()
# image.save("C:/Downloads/myimage_new.jpeg", "JPEG")  # to save the image