精英干员瑕光 2023-06-09 13:58 采纳率: 76.5%
浏览 39
已结题

python修改gif尺寸

使用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()
  • 写回答

2条回答 默认 最新

  • 大师兄6668 新星创作者: python技术领域 2023-06-09 14:12
    关注

    看到你这段代码中使用了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()函数保存到原始图片位置。这样,在下一次加载动图时,就会按照新的尺寸进行显示了。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月21日
  • 创建了问题 6月9日