天天看點

PIL Image to bytes

import io

from PIL import Image
# resp = requests.get(url)
# if resp.status_code == 200:
#        _im = BytesIO(resp.content)
#        im = PIL.Image.open(_im)

im = Image.open('./lihao.png')
# file_size = str(len(im.fp.read())) # get file size
im = im.resize((100, 200))
# im = im.resize((int(_image.width * scale), int(_image.height * scale)))
# im = im.crop((x1, y1, x1 + width, y1 + height))

# Image to bytes
imgByteArr = io.BytesIO()
im.show()
im.save(imgByteArr, format='PNG') # format: PNG / JPEG
imgByteArr = imgByteArr.getvalue()
print(len(imgByteArr))