压缩二进制图像(PNG转JPG)
Image.save()
进 BytesIO 对象再读出来即可
from PIL import Image
from io import BytesIO
img = Image.open('1.png')
img = img.convert('RGB')
bytesIO = BytesIO()
img.save(bytesIO, format='JPEG', quality=75) # 图像质量默认为75
img = bytesIO.getvalue()
with open('result.jpg', 'wb') as f:
f.write(img)
from PIL import Image
from io import BytesIO
def PIL_compress(im):
'''PIL图像压缩
:param im: PIL图像,PIL.Image
:return: bytes图像
'''
bytesIO = BytesIO()
im = im.convert('RGB')
im.save(bytesIO, format='JPEG', quality=75) # 图像质量默认为75
return bytesIO.getvalue()
if __name__ == '__main__':
img = Image.open('1.png')
img = PIL_compress(img)
with open('result.jpg', 'wb') as f:
f.write(img)