Welcome, guest | Sign In | My Account | Store | Cart

Demonstrates saving an animation done on Tkinter canvas as a series of jpeg images

Python, 39 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from Tkinter import *
from PIL import ImageGrab
from numpy import array
import os

class App(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.overrideredirect(1) # FRAMELESS CANVAS WINDOW

        self.width = 900
        self.height = 640
        self.initialize()

    def initialize(self):
        self.c = Canvas(self, width=self.width, height=self.height, background='white')
        self.c.pack(side=RIGHT, expand=YES, fill=BOTH)
        self.update() # DISPLAY THE CANVAS

        self.run_anim() # RUN ANIMATION METHOD
        self.destroy() # CLOSE THE FRAMELESS CANVAS WHEN COMPLETED

    def run_anim(self):
        print os.getcwd() # REMINDER OF THE CURRENT SAVE LOCATION
        c = self.c
        polyo = array([34,60,226,15,419,60,359,151,91,151])
        polyd = array([205,253,296,187,388,253,353,360,239,360])
        trantime = 20 # NUMBER OF FRAMES TO WRITE (TRANSITION TIME)
        for i in xrange(trantime):
            c.delete('pol')
            ptrans = (float(i)/trantime)*polyd+(trantime-float(i))/trantime*polyo
            c.create_polygon(list(ptrans), outline='black', fill='red', tags='pol')

            self.update() # UPDATE THE CANVAS DISPLAY
            savename = 'im_{0:0>6}'.format(i)
            ImageGrab.grab((0,0,self.width,self.height)).save(savename + '.jpg')

app = App(None)
app.mainloop()

Canvas only has a postscript save option. Using a frameless canvas tucked into a corner and PIL's ImageGrab, an image or animated series can be saved easily as *.jpg or other PIL format. I then use ImageMagick to create an animated GIF from the series.