使用wxpython向面板插入动图,但是尺寸不太符合要求,尝试使用以下代码改变gif尺寸但是无效。
def aaa(self,event):
if not self.teams:
wx.MessageBox('抽奖次数已用完,请重新启动!')
return
self.bb=wx.Frame(self,-1,style=wx.SIMPLE_BORDER | wx.TRANSPARENT_WINDOW )#,size=(1500,1024)
self.animation = AnimationCtrl(self.bb,size=(1000,800))
# self.animation.LoadFile(r'C:\Users\gztsrayz\Desktop\img\new.gif')
self.animation.Load(self.pngSize(r'C:\Users\gztsrayz\Desktop\img\new.gif',1000,1000))
self.animation.Play()
self.bb.Fit()
self.bb.Center()
self.bb.Show()
b = threading.Thread(target=self.cc, daemon=True)
b.start()
def cc(self):
time.sleep(2.65)
# time.sleep(.5)
self.bb.Destroy()
from PIL import Image, ImageSequence
def fk():
gifPath = r'C:\Users\gztsrayz\Desktop\img\d7c.gif'
oriGif = Image.open(gifPath)
lifeTime = oriGif.info['duration']
imgList = []
for i in ImageSequence.Iterator(oriGif):
print(i.copy())
imgList.append(i.copy())
for index, f in enumerate(imgList):
f.save(r'C:\Users\gztsrayz\Desktop\img\ff\%d.png' % index)
def pj():
gifPath = r'C:\Users\gztsrayz\Desktop\img\d7c.gif'
oriGif = Image.open(gifPath)
lifeTime = oriGif.info['duration']
imgList = []
imgNew = []
for i in ImageSequence.Iterator(oriGif):
print(i.copy())
imgList.append(i.copy())
for index, f in enumerate(imgList):
f.save(r'C:\Users\gztsrayz\Desktop\img\ff\%d.png' % index)
img = Image.open(r'C:\Users\gztsrayz\Desktop\img\ff\%d.png' % index)
# img.thumbnail((1800, 1200), Image.LANCZOS)
img.resize((1200,1200))
imgNew.append(img)
imgNew[0].save(r"C:\Users\gztsrayz\Desktop\img\new.gif", 'gif', save_all=True, append_images=imgNew[1:], loop=0,
duration=lifeTime)
oriGif.close()
看到你这段代码中使用了Pillow
库来改变GIF图片的尺寸。但是在具体的改变过程中,你没有将图片的改变结果进行保存,也就是说,即便你调用了resize()
函数,但是原始的图片并没有被修改,因此在加载动图的过程中还是会按照原来的尺寸进行显示。
要解决这个问题,只需要在调用resize()
函数之后,将修改后的图片重新保存即可。修改后的代码如下所示:
def pj():
gifPath = r'C:\Users\gztsrayz\Desktop\img\d7c.gif'
oriGif = Image.open(gifPath)
lifeTime = oriGif.info['duration']
imgList = []
imgNew = []
for i in ImageSequence.Iterator(oriGif):
print(i.copy())
imgList.append(i.copy())
for index, f in enumerate(imgList):
f.save(r'C:\Users\gztsrayz\Desktop\img\ff\%d.png' % index)
img = Image.open(r'C:\Users\gztsrayz\Desktop\img\ff\%d.png' % index)
# img.thumbnail((1800, 1200), Image.LANCZOS)
img_resize = img.resize((1200, 1200)) # 调整大小
img_resize.save(r'C:\Users\gztsrayz\Desktop\img\ff\%d.png' % index) # 保存修改后的图片
imgNew.append(img_resize)
imgNew[0].save(
r"C:\Users\gztsrayz\Desktop\img\new.gif",
'gif',
save_all=True,
append_images=imgNew[1:],
loop=0,
duration=lifeTime
)
oriGif.close()
在新的代码中,我使用了resize()
函数来进行尺寸的调整,并将调整后的结果重新用save()
函数保存到原始图片位置。这样,在下一次加载动图时,就会按照新的尺寸进行显示了。
那我自己的图为例,首先来看动图效果:
可以使用PIL库来改变gif动画的尺寸,具体步骤如下:
1.导入需要使用的模块:
from PIL import Image
2.打开gif动画,将每一帧进行等比例缩放,并保存到列表中:
im = Image.open("animated.gif") # 打开gif动画
images = []
try:
while True:
# 将每一帧进行等比例缩放
gif_frame = im.copy()
gif_frame.thumbnail((width, height), Image.ANTIALIAS)
images.append(gif_frame)
im.seek(len(images)) # 跳到下一帧
except EOFError:
pass
3.将缩放后的每一帧重新合成为一个gif动画:
images[0].save("resized.gif", save_all=True, append_images=images[1:], duration=im.info["duration"], loop=0)
完整的代码示例:
from PIL import Image
def resize_gif(filename, width, height):
im = Image.open(filename) # 打开gif动画
images = []
try:
while True:
# 将每一帧进行等比例缩放
gif_frame = im.copy()
gif_frame.thumbnail((width, height), Image.ANTIALIAS)
images.append(gif_frame)
im.seek(len(images)) # 跳到下一帧
except EOFError:
pass
# 将缩放后的每一帧重新合成为一个gif动画
images[0].save("resized.gif", save_all=True, append_images=images[1:], duration=im.info["duration"], loop=0)
# 调用函数并传入参数
resize_gif("animated.gif", 200, 200)
其中,filename
为需要缩放的gif动画文件名,width
和height
为需要缩放的尺寸。可以根据需要进行调整。注意,PIL库需要安装,可以使用pip install pillow
命令进行安装。