Анимация GIF без использования PIL - PullRequest
0 голосов
/ 25 февраля 2019

В настоящее время я пытаюсь облегчить жизнь, заставив код вставить все кадры в мой код и выполнить анимацию.Мой текущий код:

import time
from tkinter import *
import os



root = Tk()


imagelist = []
for file in os.listdir("MY-DIRECTORY"):
    if file.endswith(".gif"):
        imagelist.append(PhotoImage(file=str(os.path.join("MY-DIRECTORY", file))))


# extract width and height info
photo = PhotoImage(file=imagelist[0])
width = photo.width()
height = photo.height()
canvas = Canvas(width=width, height=height)
canvas.pack()
# create a list of image objects
giflist = []
for imagefile in imagelist:
    photo = PhotoImage(file=imagefile)
    giflist.append(photo)
# loop through the gif image objects for a while
for k in range(0, 1000):
    for gif in giflist:
        canvas.create_image(width / 2.0, height / 2.0, image=gif)
        canvas.update()
        time.sleep(0.1)
root.mainloop()

Когда я пытаюсь выполнить файл, он дает мне эту ошибку, которую я не могу понять.

Traceback (most recent call last):
  File "C:/Users/Profile/Desktop/folder (2.22.2019)/animation2.py", line 21, in <module>
    photo = PhotoImage(file=imagelist[0])
  File "C:\Users\Profile\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3545, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Profile\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3501, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "pyimage1": no such file or directory

Примечание. Я адаптирую этот код в соответствии со своими потребностями. Я его не написал.

Я запустил несколько операторов print, чтобы убедиться, что изображение загружается в массив, но не могувыясните, почему он говорит, что такого файла или каталога нет, если он уже загружен в массив.Можете ли вы все, пожалуйста, пролить свет.

1 Ответ

0 голосов
/ 25 февраля 2019

Я обнаружил, что создаю ненужный массив объектов ниже в коде.giflist[].В конечном итоге я решил проблему, удалив ее и заставив цикл использовать массив, созданный ранее в коде imagelist.Следующий код работает сейчас.

import time
from tkinter import *
import os

root = Tk()  

imagelist = []
for file in os.listdir("My-Directory"):
    if file.endswith(".gif"):
        imagelist.append(PhotoImage(file=str(os.path.join("My-Directory", file))))

# Extract width and height info
photo = PhotoImage(file="My-Directory")
width = photo.width()
height = photo.height()
canvas = Canvas(width=width, height=height)
canvas.pack()

# Loop through the gif image objects for a while
for k in range(0, len(imagelist)):
    for gif in imagelist:
        canvas.create_image(width / 2.0, height / 2.0, image=gif)
        canvas.update()
        time.sleep(0.1)
root.mainloop()
...