473,703 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6819

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.sa ve() is probably the thing you want.
>>help(pygame.i mage)
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.fr ombuffer(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.fr omstring(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.ge t_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.lo ad(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.sa ve(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.to string(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
16126
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", pygame.mixer.get_num_channels() pygame.mixer.music.set_volume(1.0) pygame.mixer.music.load('file1.mp3)
3
3716
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 Pygame to manage the image window, as it was the easiest way to implement the functionality I needed. The surrounding interface windows (there are two) are constructed with Tkinter. Despite their unholy union, Pygame and Tkinter seem, generally,...
2
2075
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 import time import pygame pygame.init() screen = pygame.display.set_mode((640,480)) pygame.display.set_caption("A Bug's Life")
1
3232
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. This is on a system running OS 10.3.9. I have modified/written a small piece of code that initializes the joystick, and pygame does recognize it. I was wondering if someone has a small snippet of code to get me going? I have posted the code...
0
1454
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 widget, which is manualy added into a window) My problem: Since adding the gtk half, it is realy slow. I can make one or the other update realy fast, or I can make it so the pygame window/widget gets a fluctuation in framerate between realy...
11
3620
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 bit long but it's straightforward.Please help import pygame from pygame.locals import * pygame.init() screen=pygame.display.set_mode((900,900)) class Hero(pygame.sprite.Sprite): def __init__(self): ...
3
4416
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 this: http://www.pygame.org/wiki/FrequentlyAskedQuestions bu it doesnt work, or maybe im doing it wrong. heres the code without the added tutorial exit:
11
11675
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 should be there// pygame.mixer.init(22050, -16, 2, 3072) pygame.mixer.music.load("example1.mp3")
5
3029
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() screen.fill(screencolor) pygame.draw.circle(screen, linecolor, (500, 20), 12, 0)
0
8758
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8669
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9121
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4432
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3123
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2450
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.