使用 PIL 模块。更多信息参见此处的答案:将图像字节数据流解码为 JPEG
from PIL import Image
from io import BytesIO
with open('img.jpg', 'rb') as f:
data = f.read()
# Load image from BytesIO
im = Image.open(BytesIO(data))
# Display image
im.show()
# Save the image to 'result.FORMAT', using the image format
im.save('result.{im_format}'.format(im_format=im.format))
Run Code Online (Sandbox Code Playgroud)