Pygame Home ||
Help Contents ||
Reference Index ||
Cdrom || Color || Cursors || Display || Draw || Event || Font || Image || Joystick || Key || Mask || Mixer || Mouse || Movie || Music || Overlay || Pixelarray || Pygame || Rect || Scrap || Sndarray || Sprite || Surface || Surfarray || Time || Transform
|pygame.image.load - load new image from a file||load new image from a file|
|pygame.image.save - save an image to disk||save an image to disk|
|pygame.image.get_extended - test if extended image formats can be loaded||test if extended image formats can be loaded|
|pygame.image.tostring - transfer image to string buffer||transfer image to string buffer|
|pygame.image.fromstring - create new Surface from a string buffer||create new Surface from a string buffer|
|pygame.image.frombuffer - create a new Surface that shares data inside a string buffer||create a new Surface that shares data inside a string buffer|
The image module contains functions for loading and saving pictures, as well as transferring Surfaces to formats usable by other packages.
Note that there is no Image class; an image is loaded as a Surface object. The Surface class allows manipulation (drawing lines, setting pixels, capturing regions, etc.).
The image module is a required dependency of Pygame, but it only optionally supports any extended file formats. By default it can only load uncompressed BMP images. When built with full image support, the pygame.image.load - load new image from a file function can support the following formats.
Saving images only supports a limited set of formats. You can save to the following formats.
PNG, JPEG saving new in pygame 1.8.
Load an image from a file source. You can pass either a filename or a Python file-like object.
Pygame will automatically determine the image type (e.g., GIF or bitmap) and create a new Surface object from the data. In some cases it will need to know the file extension (e.g., GIF images should end in ".gif"). If you pass a raw file-like object, you may also want to pass the original filename as the namehint argument.
The returned Surface will contain the same color format, colorkey and alpha transparency as the file it came from. You will often want to call Surface.convert - change the pixel format of an image with no arguments, to create a copy that will draw more quickly on the screen.
For alpha transparency, like in .png images use the convert_alpha() method after loading so that the image has per pixel transparency.
Pygame may not always be built to support all image formats. At minimum it will support uncompressed BMP. If pygame.image.get_extended - test if extended image formats can be loaded returns 'True', you should be able to load most images( including png, jpg and gif ).
You should use os.path.join() for compatibility.
eg. asurf = pygame.image.load(os.path.join('data', 'bla.png'))
This will save your Surface as either a BMP, TGA, PNG, or JPEG image. If the filename extension is unrecognized it will default to TGA. Both TGA, and BMP file formats create uncompressed files.
PNG, JPEG saving new in pygame 1.8.
If pygame is built with extended image formats this function will return True. It is still not possible to determine which formats will be available, but generally you will be able to load them all.
Creates a string that can be transferred with the 'fromstring' method in other Python imaging packages. Some Python image packages prefer their images in bottom-to-top format (PyOpenGL for example). If you pass True for the flipped argument, the string buffer will be vertically flipped.
The format argument is a string of one of the following values. Note that only 8bit Surfaces can use the "P" format. The other formats will work for any Surface. Also note that other Python image packages support more formats than Pygame.
This function takes arguments similar to pygame.image.tostring - transfer image to string buffer. The size argument is a pair of numbers representing the width and height. Once the new Surface is created you can destroy the string buffer.
The size and format image must compute the exact same size as the passed string buffer. Otherwise an exception will be raised.
See the pygame.image.frombuffer - create a new Surface that shares data inside a string buffer method for a potentially faster way to transfer images into Pygame.
Create a new Surface that shares pixel data directly from the string buffer. This method takes the same arguments as pygame.image.fromstring - create new Surface from a string buffer, but is unable to vertically flip the source data.
This will run much faster than pygame.image.fromstring, since no pixel data must be allocated and copied.