Why resizing an image in pillow isn't working?

Viewed 444

I'm converting a gif into image sequences and that's working, but I want to resize the images then save them, I put the maximum width and height and I calculated the ratio and then resize the images, but the images are the same as before, here's my code:

from PIL import Image, GifImagePlugin

imageObject = Image.open("my.gif")

print(imageObject.n_frames, "frames")
print(imageObject.size)

count = 1
max_wh = 300 #the maximum height and width
width, height = imageObject.size
ratio = min(max_wh/width, max_wh/height)

print(height, width, ratio, int(width*ratio))

for frame in range(0, imageObject.n_frames):
    imageObject.seek(frame)
    imageObject.resize((int(width*ratio), int(height*ratio)), Image.ANTIALIAS)
    imageObject.save(f"a_{count}.png")
    count += 1
1 Answers

From the Pillow documentation:

Image.resize(size, resample=3, box=None, reducing_gap=None)

Returns a resized copy of this image.

from PIL import Image, GifImagePlugin

imageObject = Image.open("my.gif")

print(imageObject.n_frames, "frames")
print(imageObject.size)

max_wh = 300 # the maximum height and width
width, height = imageObject.size
ratio = min(max_wh/width, max_wh/height)

print(height, width, ratio, int(width*ratio))

for frame in range(0, imageObject.n_frames):
    imageObject.seek(frame)
    # Image.resize() returns a resized copy of the original
    resized = imageObject.resize((int(width*ratio), int(height*ratio)), Image.ANTIALIAS)
    resized.save(f"a_{frame}.png")
Related