PIL_Image对象转为字节流
- 1、im为Image对象,可以是图片路径名称, binary_content为im转为字节流的结果
from PIL import Image
from io import BytesIO
im = Image.open("code.jpg")
img_byte = BytesIO()
im.save(img_byte, format='JPEG')
binary_content = img_byte.getvalue()
- 2、im为Image对象,可以是图片图片二进制流, binary_content为im转为字节流的结果
from PIL import Image
from io import BytesIO
import requests
url = "图片链接的url"
response = requests.get(url)
im = Image.open(BytesIO(response.content))
img_byte = BytesIO()
im.save(img_byte, format='JPEG')
binary_content = img_byte.getvalue()