1

I wanted to use Python to create animations (video) containing text and simple moving geometric objects (lines, rectangles, circles and so on).

In the book titled "Python 2.6 Graphics Cookbook" I found examples using Tkinter library. First, it looked like what I need. I was able to create simple animation but then I realized that in the end I want to have a file containing my animation (in gif or mp4 format). However, what I have, is an application with GUI running on my computer and showing me my animation.

Is there a simple way to save the animation that I see in my GUI in a file?

Roman
  • 124,451
  • 167
  • 349
  • 456

4 Answers4

3

There is no simple way.

The question Programmatically generate video or animated GIF in Python? has answers related strictly to creating these files with python (ie: it doesn't mention tkinter).

The question How can I convert canvas content to an image? has answers related to saving the canvas as an image

You might be able to take the best answers from those two questions and combine them into a single program.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
2

I've accomplished this before, but not in a particularly pretty way.

Tl;dr save your canvas as an image at each step of the iteration, use external tools to convert from image to gif

This won't require any external dependencies or new packages except having imagemagick already installed on your machine

  1. Save the image

I assume that you're using a Tkinter canvas object. If you're posting actual images to the tk widgets, it will probably be much easier to save them; the tk canvas doesn't have a built-in save function except as postcript. Postscript might actually be fine for making the animation, but otherwise you can

Once the images are saved, I used imagemagick to dump them into either a gif, or into a mpg. You can run the command right from python using How to run imagemagick in the background from python or something similar. It also means that the process is implictely run on a separate thread, so it won't halt your program while it happens. You can query the file to find out when the process is done.

The command

convert ../location/*.ps -quality 100 ../location/animation.gif

should do the trick.

Quirks:

There are some small details, and the process isn't perfect. Imagemagick reads files in order, so you'll need to save the files so that alphabetical and chronological line up. Beware that the name

name9.ps

Is alphabetically greater than

name10.ps

From imagemagick's point of view.

If you don't have imagemagick, you can download it easily (its a super useful command-line tool to have) on linux and mac, and cygwin comes with it on windows. If you're worried about portability... well... PIL isn't standard either

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46
0

There is a way of doing that, with the "recording screen method", this was explained in other question: "how can you record your screen in a gif?".

Click the link -->LICEcap : https://github.com/lepht/licecap

They say that it's free software for Mac (OS X) and Windows

Community
  • 1
  • 1
jenko_cp
  • 103
  • 1
  • 8
0

You could look at Panda3D, but it could be a little over killed for what you need.

I would say you can use Blender3d too but i'm not really sure of how it works. Someone more experimented then me could tell you more about this.

Matei
  • 1,783
  • 1
  • 14
  • 17