473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to save a screenshot in pygame?

hi -- sorry if this is trivial -- but how do you make a screenshot of a
pygame display? i have a surface which is basically the entire visible
screen -- how do you write this surface as an image file during
specific events in the script execution? image format doesnt matter.

thanks! christine

Aug 8 '06 #1
1 6739

liuliuliu wrote:
hi -- sorry if this is trivial -- but how do you make a screenshot of a
pygame display? i have a surface which is basically the entire visible
screen -- how do you write this surface as an image file during
specific events in the script execution? image format doesnt matter.
pygame.image.save() is probably the thing you want.
>>help(pygame.image)
Help on module pygame.image in pygame:

NAME
pygame.image

FILE
c:\python24\lib\site-packages\pygame\image.pyd

DESCRIPTION
This module contains functions to transfer images in and out
of Surfaces. At the minimum the included load() function will
support BMP files. If SDL_image is properly installed when
pygame is installed, it will support all the formats included
with SDL_image. You can call the get_extended() function to test
if the SDL_image support is available.

Some functions that communicate with other libraries will require
that those libraries are properly installed. For example, the
save()
function can only save OPENGL surfaces if pyopengl is available.

FUNCTIONS
frombuffer(...)
pygame.image.frombuffer(string, size, format) -Surface
create a surface from a python memory buffer

This works like the fromstring() method, but uses Python
buffer objects. It is about 20 times faster than fromstring().
Strings and memory maps are examples of buffers in Python.

See the fromstring() function for information about the size
and format arguments.

fromstring(...)
pygame.image.fromstring(string, size, format, flipped=0) ->
Surface
create a surface from a raw string buffer

This will create a new Surface from a copy of raw data in
a string. This can be used to transfer images from other
libraries like PIL's fromstring().

In most cases you can use the frombuffer() which accepts
strings
and is about 20 times faster.

The flipped argument should be set to true if the image in
the string is.

The format argument is a string representing which type of
string data you need. It can be one of the following, "P"
for 8bit palette indices. "RGB" for 24bit RGB data, "RGBA"
for 32bit RGB and alpha, or "RGBX" for 32bit padded RGB colors.
"ARGB" is a popular format for big endian platforms.

These flags are a subset of the formats supported the PIL
Python Image Library. Note that the "P" format only create
an 8bit surface, but the colormap will be all black.

get_extended(...)
pygame.image.get_extended() -int
returns true if SDL_image formats are available

This will return a true value if the extended image formats
from SDL_image are available for loading.

load = load_extended(...)

load_basic(...)
pygame.image.load(file, [namehint]) -Surface
load an image to a new Surface

This will load an image into a new surface. You can pass it
either a filename, or a python file-like object to load the
image
from. If you pass a file-like object that isn't actually a file
(like the StringIO class), then you might want to also pass
either the filename or extension as the namehint string. The
namehint can help the loader determine the filetype.

If pygame was installed without SDL_image support, the load
will only work with BMP images. You can test if SDL_image is
available with the get_extended() function. These extended
file formats usually include GIF, PNG, JPG, PCX, TGA, and more.

If the image format supports colorkeys and pixel alphas, the
load() function will properly load and configure these types
of transparency.

load_extended(...)

save(...)
pygame.image.save(Surface, file) -None
save surface data

This will save your surface as a BMP or TGA image. The given
file argument can be either a filename or a python file-like
object. This will also work under OPENGL display modes.

The image will default to save with the TGA format. If the
filename has the BMP extension, it will use the BMP format.

tostring(...)
pygame.image.tostring(Surface, format, flipped=0) -string
create a raw string buffer of the surface data

This will copy the image data into a large string buffer.
This can be used to transfer images to other libraries like
PIL's fromstring() and PyOpenGL's glTexImage2D().

The flipped argument will cause the output string to have
it's contents flipped vertically.

The format argument is a string representing which type of
string data you need. It can be one of the following, "P"
for 8bit palette indices. "RGB" for 24bit RGB data, "RGBA"
for 32bit RGB and alpha, or "RGBX" for 32bit padded RGB colors.
"ARGB" is a popular format for big endian platforms.

These flags are a subset of the formats supported the PIL
Python Image Library. Note that the "P" format only will
work for 8bit Surfaces.

If you ask for the "RGBA" format and the image only has
colorkey data. An alpha channel will be created from the
colorkey values.
>
thanks! christine
Aug 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Marian Aldenhövel | last post by:
Hi, I am trying to make pygame play music on windows. This simple program: import pygame,time pygame.init() print "Mixer settings", pygame.mixer.get_init() print "Mixer channels",...
3
by: Tim Knauf | last post by:
Hi everyone, I'm glad to have found this list. I've written a small script for my own use which, amongst other things, captures mouse click information from a window containing an image. I used...
2
by: Brent W. Hughes | last post by:
I'm just starting to learn pygame. I write what I think is just about the simplest program that should display a window and then quit. #----------------------------------------------- import sys...
1
by: kjm | last post by:
Hi everyone, I have recently acquired a Logitech Rumble pad to use as an input device. I have been having trouble getting the event que to respond that a button or hat arrow has been pressed. ...
0
by: Lunpa | last post by:
My project: I'm working on a game, where in the ui, it takes the pygame window, and shoves it into a gtk2 socket widget. (gtk2 widgets are generated with glade, with the exception of the socket...
11
by: dynamo | last post by:
Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not do what it is supposed to do either.the code is a...
3
by: globalrev | last post by:
im doing this : http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/ and when closing the program the window stays up and doesnt respond. i tried adding...
11
by: globalrev | last post by:
http://www.pygame.org/docs/ref/mixer.html import pygame #pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=3072) //it complained abiout words= so i guess its only the nbrs...
5
by: defn noob | last post by:
Im using PyGame to draw images of graphs and trees. Howver right now i am looping using: while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.