简体   繁体   中英

Why resizing an image in pillow isn't working?

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM