.. include:: common.txt
:mod:`pygame.Surface`
=====================
.. currentmodule:: pygame
.. class:: Surface
| :sl:`pygame object for representing images`
| :sg:`Surface((width, height), flags=0, depth=0, masks=None) -> Surface`
| :sg:`Surface((width, height), flags=0, Surface) -> Surface`
A pygame Surface is used to represent any image. The Surface has a fixed
resolution and pixel format. Surfaces with 8-bit pixels use a color palette
to map to 24-bit color.
Call ``pygame.Surface()`` to create a new image object. The Surface will be
cleared to all black. The only required arguments are the sizes. With no
additional arguments, the Surface will be created in a format that best
matches the display Surface.
The pixel format can be controlled by passing the bit depth or an existing
Surface. The flags argument is a bitmask of additional features for the
surface. You can pass any combination of these flags:
::
HWSURFACE, creates the image in video memory
SRCALPHA, the pixel format will include a per-pixel alpha
Both flags are only a request, and may not be possible for all displays and
formats.
Advance users can combine a set of bitmasks with a depth value. The masks
are a set of 4 integers representing which bits in a pixel will represent
each color. Normal Surfaces should not require the masks argument.
Surfaces can have many extra attributes like alpha planes, colorkeys, source
rectangle clipping. These functions mainly effect how the Surface is blitted
to other Surfaces. The blit routines will attempt to use hardware
acceleration when possible, otherwise they will use highly optimized
software blitting methods.
There are three types of transparency supported in pygame: colorkeys,
surface alphas, and pixel alphas. Surface alphas can be mixed with
colorkeys, but an image with per pixel alphas cannot use the other modes.
Colorkey transparency makes a single color value transparent. Any pixels
matching the colorkey will not be drawn. The surface alpha value is a single
value that changes the transparency for the entire image. A surface alpha of
255 is opaque, and a value of 0 is completely transparent.
Per pixel alphas are different because they store a transparency value for
every pixel. This allows for the most precise transparency effects, but it
also the slowest. Per pixel alphas cannot be mixed with surface alpha and
colorkeys.
There is support for pixel access for the Surfaces. Pixel access on hardware
surfaces is slow and not recommended. Pixels can be accessed using the
``get_at()`` and ``set_at()`` functions. These methods are fine for simple
access, but will be considerably slow when doing of pixel work with them. If
you plan on doing a lot of pixel level work, it is recommended to use a
:class:`pygame.PixelArray`, which gives an array like view of the surface.
For involved mathematical manipulations try the :mod:`pygame.surfarray`
module (It's quite quick, but requires NumPy.)
Any functions that directly access a surface's pixel data will need that
surface to be lock()'ed. These functions can ``lock()`` and ``unlock()`` the
surfaces themselves without assistance. But, if a function will be called
many times, there will be a lot of overhead for multiple locking and
unlocking of the surface. It is best to lock the surface manually before
making the function call many times, and then unlocking when you are
finished. All functions that need a locked surface will say so in their
docs. Remember to leave the Surface locked only while necessary.
Surface pixels are stored internally as a single number that has all the
colors encoded into it. Use the ``Surface.map_rgb()`` and
``Surface.unmap_rgb()`` to convert between individual red, green, and blue
values into a packed integer for that Surface.
Surfaces can also reference sections of other Surfaces. These are created
with the ``Surface.subsurface()`` method. Any change to either Surface will
effect the other.
Each Surface contains a clipping area. By default the clip area covers the
entire Surface. If it is changed, all drawing operations will only effect
the smaller area.
.. method:: blit
| :sl:`draw one image onto another`
| :sg:`blit(source, dest, area=None, special_flags = 0) -> Rect`
Draws a source Surface onto this Surface. The draw can be positioned with
the dest argument. Dest can either be pair of coordinates representing
the upper left corner of the source. A Rect can also be passed as the
destination and the topleft corner of the rectangle will be used as the
position for the blit. The size of the destination rectangle does not
effect the blit.
An optional area rectangle can be passed as well. This represents a
smaller portion of the source Surface to draw.
An optional special flags is for passing in new in 1.8.0: ``BLEND_ADD``,
``BLEND_SUB``, ``BLEND_MULT``, ``BLEND_MIN``, ``BLEND_MAX`` new in 1.8.1:
``BLEND_RGBA_ADD``, ``BLEND_RGBA_SUB``, ``BLEND_RGBA_MULT``,
``BLEND_RGBA_MIN``, ``BLEND_RGBA_MAX`` ``BLEND_RGB_ADD``,
``BLEND_RGB_SUB``, ``BLEND_RGB_MULT``, ``BLEND_RGB_MIN``,
``BLEND_RGB_MAX`` With other special blitting flags perhaps added in the
future.
The return rectangle is the area of the affected pixels, excluding any
pixels outside the destination Surface, or outside the clipping area.
Pixel alphas will be ignored when blitting to an 8 bit Surface.
special_flags new in pygame 1.8.
For a surface with colorkey or blanket alpha, a blit to self may give
slightly different colors than a non self-blit.
.. ## Surface.blit ##
.. method:: convert
| :sl:`change the pixel format of an image`
| :sg:`convert(Surface) -> Surface`
| :sg:`convert(depth, flags=0) -> Surface`
| :sg:`convert(masks, flags=0) -> Surface`
| :sg:`convert() -> Surface`
Creates a new copy of the Surface with the pixel format changed. The new
pixel format can be determined from another existing Surface. Otherwise
depth, flags, and masks arguments can be used, similar to the
``pygame.Surface()`` call.
If no arguments are passed the new Surface will have the same pixel
format as the display Surface. This is always the fastest format for
blitting. It is a good idea to convert all Surfaces before they are
blitted many times.
The converted Surface will have no pixel alphas. They will be stripped if
the original had them. See ``Surface.convert_alpha()`` for preserving or
creating per-pixel alphas.
The new copy will have the same class as the copied surface. This lets
as Surface subclass inherit this method without the need to override,
unless subclass specific instance attributes also need copying.
.. ## Surface.convert ##
.. method:: convert_alpha
| :sl:`change the pixel format of an image including per pixel alphas`
| :sg:`convert_alpha(Surface) -> Surface`
| :sg:`convert_alpha() -> Surface`
Creates a new copy of the surface with the desired pixel format. The new
surface will be in a format suited for quick blitting to the given format
with per pixel alpha. If no surface is given, the new surface will be
optimized for blitting to the current display.
Unlike the ``Surface.convert()`` method, the pixel format for the new
image will not be exactly the same as the requested source, but it will
be optimized for fast alpha blitting to the destination.
As with ``Surface.convert()`` the returned surface has the same class as
the converted surface.
.. ## Surface.convert_alpha ##
.. method:: copy
| :sl:`create a new copy of a Surface`
| :sg:`copy