如何从python 3.8.6的gif文件中获得帧数?



我正试图从gif文件夹中加载一个gif,这是由一个txt文档上的数字选择的。它正在选择正确的一个,但我不确定如何从选定的gif中获得帧数。下面是到目前为止的代码:

from tkinter import *
from PIL import Image, ImageTk
from time import sleep
import os
root = Tk()
root.title("displaygif")
root.geometry("404x484")
with open('wantedgif/wantedgif.txt') as wantedgif:        #This is the file where the ID for the gif is
gifID = wantedgif.readline(3)
print (gifID)
line_number = 0
with open("CodeRef/gifIDList.txt", 'r') as read_obj:    #This is the file that has the ID and what 
the gif names are, under there respective ID
for line in read_obj:
line_number += 1
if gifID in line:
line_number -= 1
gif = read_obj.readlines()
print(gif[line_number])                     #This all seems to work fine
im = Image.open('gif/' + gif[line_number].strip() + '.gif')    #Opens the gif using the gif name

我不确定如何计算gif中的帧数,以及如何显示它。我已经找了几个小时了,但是找不到任何有用的东西。最好不要导入任何其他内容,但这并不重要。提前谢谢。

您可以使用im.n_frames来获取GIF图像中的帧数。

最新更新