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

如何从python 3.8.6中的gif文件中获取帧数?,python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我正在尝试从gif文件夹中加载gif,该文件夹由txt文档上的数字选择。它选择的是正确的,但我不确定如何从所选的gif中获得帧数。 以下是迄今为止的代码: from tkinter import * from PIL import Image, ImageTk from time import sleep import os root = Tk() root.title("displaygif") root.geome

我正在尝试从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\u frames
获取GIF图像中的帧数。

那么你发布的代码实际上与你的实际问题基本无关?怎么会这样?你正在从文本文件中读取一堆标签;这些操作与您所问的问题完全无关。