Skip to content Skip to sidebar Skip to footer

Convert Pillow Image Object To Jpegimagefile Object

I cropped an jpeg image, but the cropped image type is how can i convert it to ? thank you! i

Solution 1:

If you do not want a pyhsical file, do use a memory file:

import requests
from PIL import Image
from io import BytesIO    

img = Image.open(BytesIO(requests.get("https://mamahelpers.co/assets/images/faq/32B.JPG").content))
img2 = img.crop((1,20,50,80))

b = BytesIO()
img2.save(b,format="jpeg")
img3 = Image.open(b)

print(type(img))  # <class 'PIL.JpegImagePlugin.JpegImageFile'>print(type(img2)) # <class 'PIL.Image.Image'> print(type(img3)) # <class 'PIL.JpegImagePlugin.JpegImageFile'>

ByteIO is a stream-obj, it is probably wise to close() it at some point when no longer needed.

Post a Comment for "Convert Pillow Image Object To Jpegimagefile Object"