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

How do I get the number of frames from a gif file in python 3.8.6?

我正在尝试从一个 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图片内的帧数。