Discussion:
pygame: output to file?
superpollo
2012-04-16 09:34:40 UTC
Permalink
HI.

is there a way to convert the graphical output of a pygame application
to a mpeg file or better an animated gif? i mean, not using an external
capture program...

bye
alex23
2012-04-16 11:49:46 UTC
Permalink
Post by superpollo
is there a way to convert the graphical output of a pygame application
to a mpeg file or better an animated gif? i mean, not using an external
capture program...
There is, but it's probably not going to be as performant as using
something external:

http://stackoverflow.com/questions/6087484/how-to-capture-pygame-screen

I'd also recommend checking out the pygame mailing list:
http://www.pygame.org/wiki/info
superpollo
2012-04-16 12:37:17 UTC
Permalink
Post by alex23
Post by superpollo
is there a way to convert the graphical output of a pygame application
to a mpeg file or better an animated gif? i mean, not using an external
capture program...
There is, but it's probably not going to be as performant as using
http://stackoverflow.com/questions/6087484/how-to-capture-pygame-screen
but where in the code should i put the line:

pygame.image.save(window, "screenshot.jpeg")

?

tia

bye
superpollo
2012-04-16 13:04:31 UTC
Permalink
Post by superpollo
Post by alex23
Post by superpollo
is there a way to convert the graphical output of a pygame application
to a mpeg file or better an animated gif? i mean, not using an external
capture program...
There is, but it's probably not going to be as performant as using
http://stackoverflow.com/questions/6087484/how-to-capture-pygame-screen
pygame.image.save(window, "screenshot.jpeg")
i mean, if i would want to capture ALL the frames in a video file...

i ask this becase this line captures just one frame and overwrites over
the successive ones...

bye
Temia Eszteri
2012-04-17 07:25:05 UTC
Permalink
Okay, superpollo. I'm looking at a few possible ways to do this (the
Zen of Python hates me, even though I'm dutch-blooded!), and I'd like
to ask a couple more questions before I dive into writing up a
solution.

First, are you simply trying to capture the framebuffer of a
Pygame-made game in realtime, or are you using Pygame to explicitly
render and build a movie or GIF animation sequence? I need to know
what kind of overhead is permissible, and in the former case I know
there won't be a lot of allowance due to how slow Pygame's drawing
operations can get when they stack up, especially at high resolutions.

Secondly, how much are you willing to install to have a means to do
this? Because from the looks of things, this'll have to either be
tackled with:

* ImageMagick (http://www.imagemagick.org/) as Ian suggested - the
ctypes binding I found for this was too immature to use, and the other
option will require compiling,
* A branch someone on EsperNet pointed me to called GraphicsMagick
(http://www.graphicsmagick.org/) - haven't looked into this much so I
can't comment on it yet,
* or a module I found on Google Code that greatly extends from PIL's
meager GIF-handling abilities, images2gif
(http://code.google.com/p/visvis/source/browse/vvmovie/images2gif.py).

Let me know what you think of the options, and I'm sorry this has
gotten a little too complex. I was hoping animated GIF sequencing in
Python would be a simple matter too!

~Temia
--
When on earth, do as the earthlings do.
Temia Eszteri
2012-04-17 09:30:24 UTC
Permalink
Oh, forgot to mention some things. Also poorly phrased other things.
Let me try that again.
Post by Temia Eszteri
* ImageMagick (http://www.imagemagick.org/) as Ian suggested - the
ctypes binding I found for this was too immature to use, and the other
option will require compiling,
The 'other option' I meant here was a different Python binding,
PythonMagick. In comparison, the ctypes-based binding is called
PythonMagickWand.

Failing that, both this and GraphicsMagick can be manipulated through
shell libraries, but I haven't done anything like that yet so I'd be
going in blind and hoping for the best. (I make no claim to be an
expert or that I ever was or will be one - I'm just starting out and
helping where I can)
Post by Temia Eszteri
* or a module I found on Google Code that greatly extends from PIL's
meager GIF-handling abilities, images2gif
(http://code.google.com/p/visvis/source/browse/vvmovie/images2gif.py).
While you said you preferred animated GIFs in your opening message,
the visvis project at Google code (behind images2gif) also has
images2avi, which could possibly be used with conversion software down
the line to output to a different container and codec if you wanted to
go with the video stream.

Once again, let me know what you think. As for the other
comp.lang.python folks, please tell me if I'm overlooking something
that'll throw a spanner in the works.

~Temia
--
When on earth, do as the earthlings do.
alex23
2012-04-18 06:48:46 UTC
Permalink
Post by superpollo
i ask this becase this line captures just one frame and overwrites over
the successive ones...
So increment the image name each time.

Terry Reedy
2012-04-16 16:48:37 UTC
Permalink
Post by superpollo
Post by alex23
Post by superpollo
is there a way to convert the graphical output of a pygame application
to a mpeg file or better an animated gif? i mean, not using an external
capture program...
There is, but it's probably not going to be as performant as using
http://stackoverflow.com/questions/6087484/how-to-capture-pygame-screen
pygame.image.save(window, "screenshot.jpeg")
?
Somewhere in the loop that draws frames, perhaps just before or after
flipping to the screen. Of course, number the frames.

framenum = 0 # somewhere, just once
# in loop
pygame.image.save(window, 'frame%05d' % framenum)
framenum += 1

5 digits for frames is just an example.
Post by superpollo
Post by alex23
Post by superpollo
'frame%05d.jpg' % 21
'frame00021.jpg'
Post by superpollo
Post by alex23
Post by superpollo
'frame{:0>5d}.jpg'.format(21)
'frame00021.jpg'

You might consider saving .bmp bitmaps, as mpeg compresses across frames
as well as within frames. If you have sprites moving over a static
background, only the changes need to be encoded.
--
Terry Jan Reedy
Temia Eszteri
2012-04-16 18:39:55 UTC
Permalink
Post by Terry Reedy
Post by superpollo
Post by alex23
Post by superpollo
is there a way to convert the graphical output of a pygame application
to a mpeg file or better an animated gif? i mean, not using an external
capture program...
There is, but it's probably not going to be as performant as using
http://stackoverflow.com/questions/6087484/how-to-capture-pygame-screen
pygame.image.save(window, "screenshot.jpeg")
?
Somewhere in the loop that draws frames, perhaps just before or after
flipping to the screen. Of course, number the frames.
framenum = 0 # somewhere, just once
# in loop
pygame.image.save(window, 'frame%05d' % framenum)
framenum += 1
5 digits for frames is just an example.
Post by superpollo
Post by alex23
Post by superpollo
'frame%05d.jpg' % 21
'frame00021.jpg'
Post by superpollo
Post by alex23
Post by superpollo
'frame{:0>5d}.jpg'.format(21)
'frame00021.jpg'
You might consider saving .bmp bitmaps, as mpeg compresses across frames
as well as within frames. If you have sprites moving over a static
background, only the changes need to be encoded.
--
Terry Jan Reedy
If there's a image-handling library out there for Python that can make
animated GIFs, I might be able to come up with a faster and more
internalized solution using surface.convert() to paletted modes and
image.tostring() functions or similarmeans to pass the frames to said
library.

Problem is that I can't seem to find a library that does handle
animated GIFs. My google-fu has always been weak, alas. Does anyone
feel up to confirming whether one exists or not?

~Temia
--
When on earth, do as the earthlings do.
Ian Kelly
2012-04-16 19:01:34 UTC
Permalink
Post by Temia Eszteri
If there's a image-handling library out there for Python that can make
animated GIFs, I might be able to come up with a faster and more
internalized solution using surface.convert() to paletted modes and
image.tostring() functions or similarmeans to pass the frames to said
library.
Problem is that I can't seem to find a library that does handle
animated GIFs. My google-fu has always been weak, alas. Does anyone
feel up to confirming whether one exists or not?
ImageMagick? There are Python bindings for it.
Loading...