Created
August 28, 2015 09:02
-
-
Save Ripley6811/6f3e9d4519b1b0a2993a to your computer and use it in GitHub Desktop.
Ssaving an animation done on Tkinter canvas as a series of jpeg images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Tkinter import * | |
from PIL import ImageGrab | |
from numpy import array | |
import os | |
""" | |
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. | |
""" | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment