Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does resizing image in Pillow-python remove Image.format?

I'm resizing images in python using Pillow

image = Image.open("image_file.jpg")

print(image.format) # Prints JPEG

resized_image = image.resize([100,200],PIL.Image.ANTIALIAS)

print(resized_image.format) # Prints None!!

Why does resized_image.format Hold a None Value?

And How can i retain the format when resizing using pillow?

like image 666
wolfgang Avatar asked Mar 31 '15 16:03

wolfgang


People also ask

How does resize image work Python?

To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.


1 Answers

Because Image.resize creates a new Image object (resized copy of the image) and for any images when creating by the library itself (via a factory function, or by running a method on an existing image), the "format" attribute is set to None.

If you need the format attribute you still can to do this:

image = Image.open("image_file.jpg") #old image object
resized_image = image.resize([100,200],PIL.Image.ANTIALIAS)
resized_image.format = image.format # original image extension

Read the docs

like image 60
felipsmartins Avatar answered Oct 26 '22 12:10

felipsmartins