我正在将gif转换为图像序列,并且可以正常工作,但是我想调整图像的大小然后保存它们,我输入最大宽度和高度,并计算出比例,然后调整图像的大小,但是图像是和以前一样,这是我的代码:
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
答案 0 :(得分:1)
Image.resize(size, resample=3, box=None, reduction_gap=None)
返回此图像的调整大小副本。
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")