473,373 Members | 1,407 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,373 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 6748

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() ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.