简体   繁体   中英

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

I am trying to load a gif out of a folder of gifs, which is selected by a number on a txt document. It is selecting the correct one, but I am not sure how to get the number of frames from the selected gif. Here is the code so far:

    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

I am not sure how to count the number of frames in the gif, and how to display it either. I have searched for a few hours, but cannot find anything that works. It would be preferable to not import anything else, but it doesn't really matter. Thanks in advance.

You can use im.n_frames to get the number of frames inside the GIF image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM