2011-10-31 3 views
4

Python 이미지 라이브러리를 사용하여 Tkinter에 애니메이션 GIF를 표시 할 수있는 방법이 있습니까?python tkinter PIL을 사용하여 애니메이션 GIF 표시

저는 ImageSequence module이 그것을 할 수있는 방법이라고 생각했지만, 가능하면 그것을 사용하는 방법을 모르겠습니다.

첫 번째 질문은 쉬운 방법이 있는지입니다. 예 : PIL과 ImageSequence를 사용하여 GIF를로드하고 ImageTk.PhotoImage을 사용하여 Tkinter 창에 그리면 애니메이션이 적용됩니다.

또는 after 메서드 또는 time.sleep과 같은 함수를 사용하여 GIF 프레임을 반복하고 tkinter 창에 그려야하는 기능을 직접 설정해야합니까?

두 번째 질문 : GIF 프레임을 반복하는 함수를 만들어야하는 경우에도 ImageSequence 모듈이이를 수행해야하거나 PIL에 다른 모듈이 있습니까?

저는 topic에 표시된 Python 3.1 및 private port of PIL을 사용하고 있습니다.

답변

6

뉴스 그룹 : "프레드릭 룬트 (Fredrik Lundh)"

날짜 : 2006년 (월)

다니엘 Nogradi 쓴 5월 1일 :

'소스 배포판에서 comp.lang.python

1.1.4 버전의 디렉토리에는 animated gif를 모두 다루는 player.py, gifmaker.py 및 explode.py 이 있습니다. '

이들은 여전히 ​​1.1.5 (및 1.1.6)와 함께 제공되며 작동해야합니다. 당신이 놓치고있는 모든 스크립트 디렉토리에서 몇 개의 파일 인 경우

, 당신은 그들을 여기 를 얻을 수 있습니다 :

http://svn.effbot.org/public/pil/Scripts/


player.py 명령 줄에서 실행

이 하나가 작동하는지 확인하십시오.

from Tkinter import * 
from PIL import Image, ImageTk 


class MyLabel(Label): 
    def __init__(self, master, filename): 
     im = Image.open(filename) 
     seq = [] 
     try: 
      while 1: 
       seq.append(im.copy()) 
       im.seek(len(seq)) # skip to next frame 
     except EOFError: 
      pass # we're done 

     try: 
      self.delay = im.info['duration'] 
     except KeyError: 
      self.delay = 100 

     first = seq[0].convert('RGBA') 
     self.frames = [ImageTk.PhotoImage(first)] 

     Label.__init__(self, master, image=self.frames[0]) 

     temp = seq[0] 
     for image in seq[1:]: 
      temp.paste(image) 
      frame = temp.convert('RGBA') 
      self.frames.append(ImageTk.PhotoImage(frame)) 

     self.idx = 0 

     self.cancel = self.after(self.delay, self.play) 

    def play(self): 
     self.config(image=self.frames[self.idx]) 
     self.idx += 1 
     if self.idx == len(self.frames): 
      self.idx = 0 
     self.cancel = self.after(self.delay, self.play)   


root = Tk() 
anim = MyLabel(root, 'animated.gif') 
anim.pack() 

def stop_it(): 
    anim.after_cancel(anim.cancel) 

Button(root, text='stop', command=stop_it).pack() 

root.mainloop() 
+0

고맙지 만, 저는 Python을 처음 접했을 때 실제로 스크립트를 어떻게 사용합니까? tkinter, PIL 및 player를 가져 왔고 이미 이미지를 열었습니다.'animation = player.UI (root, im) '및 몇 가지 변형을 시도했습니다. 플레이어 스크립트의 UI 클래스가 Label입니까? pack()하려고하면 "UI 객체에는 tk 속성이 없습니다"라고 표시되고 pack() 또는 grid()를 수행하지 않으면 아무 일도 일어나지 않습니다. –