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

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日

悬赏问题

  • ¥20 c#实现打开word的功能,并且需要安装成windows服务,word打不开怎么办
  • ¥15 python用ARIMA时间预测模型预测数据出错,急!
  • ¥30 为什么后端传给前端vue的河流json数据不在地图中显示出来
  • ¥50 关于弹性波动方程求解的问题:
  • ¥100 C# 做 10个串口数据采集;采集数据填到 EXE表中;1路与PLC MODBUS通讯 ;要求速度尽量快点; 有能做完整程序的单聊,重酬
  • ¥15 P0口接8个LED,编写程序,并绘制proteus仿真电路原理图
  • ¥15 java,maven
  • ¥15 单独编译安卓13车载evs
  • ¥20 完成实验一,要求按照图片要求,实验报告一定要按照要求,并且回答简答题,截止日期明天中午12:00
  • ¥30 孪生网络模型,当训练集与测试集共用一个数据集时,训练准确率为100%,而测试准确率仍在50%左右浮动(正常来说测试的都是学习过的数据,准确率应为100%)。【不要GPT及任何大模型回答】