3
<body bgcolor=#aaeebb text=#000000 link=#331111 vlink=#331111>
6
<table cellspacing=3 width=100%><tr><td bgcolor=#00000>
7
<table width=100%><tr><td bgcolor=c2fc20 align=center>
8
<a href=http://www.pygame.org>
9
<img src=../pygame_tiny.gif border=0 width=200 height=60></a><br>
10
<b>pygame documentation</b>
11
</td><td bgcolor=6aee28 align=center valign=top width=100%>
14
<a href=http://www.pygame.org>Home</a> ||
15
<a href=../index.html>Help Contents</a> ||
18
|| <a href=pygame.html>pygame</a> ||
19
<a href=pygame_cdrom.html>cdrom</a> ||
20
<a href=pygame_constants.html>constants</a> ||
21
<a href=pygame_display.html>display</a> ||
22
<a href=pygame_draw.html>draw</a> ||
23
<a href=pygame_event.html>event</a> ||<br>
24
|| <a href=pygame_font.html>font</a> ||
25
<a href=pygame_image.html>image</a> ||
26
<a href=pygame_joystick.html>joystick</a> ||
27
<a href=pygame_key.html>key</a> ||
28
<a href=pygame_mixer.html>mixer</a> ||
29
<a href=pygame_mixer_music.html>mixer_music</a> ||<br>
30
|| <a href=pygame_mouse.html>mouse</a> ||
31
<a href=pygame_movie.html>movie</a> ||
32
<a href=pygame_surfarray.html>surfarray</a> ||
33
<a href=pygame_time.html>time</a> ||
34
<a href=pygame_transform.html>transform</a> ||<br>
35
<br>|| <a href=CD.html>CD</a> ||
36
<a href=Channel.html>Channel</a> ||
37
<a href=Clock.html>Clock</a> ||
38
<a href=Font.html>Font</a> ||
39
<a href=Joystick.html>Joystick</a> ||
40
<a href=Movie.html>Movie</a> ||<br>
41
|| <a href=Rect.html>Rect</a> ||
42
<a href=Sound.html>Sound</a> ||
43
<a href=Surface.html>Surface</a> ||<br>
44
<br>|| <a href=pygame_cursors.html>cursors</a> ||
45
<a href=pygame_sprite.html>sprite</a> ||
46
<a href=pygame_version.html>version</a> ||<br>
49
</td></tr></table></td></tr></table>
51
<h2 align=center>Surface</h2>
52
Surface objects represent a simple memory buffer of pixels.
53
Surface objects can reside in system memory, or in special
54
hardware memory, which can be hardware accelerated. Surfaces that
55
are 8 bits per pixel use a colormap to represent their color
56
values. All Surfaces with higher bits per pixel use a packed
57
pixels to store their color values.
59
Surfaces can have many extra attributes like alpha planes,
60
colorkeys, source rectangle clipping. These functions mainly
61
effect how the Surface is blitted to other Surfaces. The blit
62
routines will attempt to use hardware acceleration when possible,
63
otherwise will use highly optimized software blitting methods.
65
There is support for pixel access for the Surfaces. Pixel access
66
on hardware surfaces is slow and not recommended. Pixels can be
67
accessed using the <a href=#get_at>get_at()</a> and <a href=#set_at>set_at()</a> functions. These methods
68
are fine for simple access, but will be considerably slow when
69
doing of pixel work with them. If you plan on doing a lot of
70
pixel level work, it is recommended to use the pygame.surfarray
71
module, which can treat the surfaces like large multidimensional
72
arrays (and it's quite quick).<p> </p>Any functions that directly access a surface's pixel data will
73
need that surface to be <a href=#lock>lock()</a>'ed. These functions can <a href=#lock>lock()</a>
74
and <a href=#unlock>unlock()</a> the surfaces themselves without assistance. But, if
75
a function will be called many times, there will be a lot of overhead
76
for multiple locking and unlocking of the surface. It is best to lock
77
the surface manually before making the function call many times, and
78
then unlocking when you are finished. All functions that need a locked
79
surface will say so in their docs.
81
Also remember that you will want to leave the surface locked for the
82
shortest amount of time needed.
85
Here is the quick breakdown of how packed pixels work (don't worry if
86
you don't quite understand this, it is only here for informational
87
purposes, it is not needed). Each colorplane mask can be used to
88
isolate the values for a colorplane from the packed pixel color.
89
Therefore PACKED_COLOR & RED_MASK == REDPLANE. Note that the
90
REDPLANE is not exactly the red color value, but it is the red
91
color value bitwise left shifted a certain amount. The losses and
92
masks can be used to convert back and forth between each
93
colorplane and the actual color for that plane. Here are the
94
final formulas used be map and unmap.
95
PACKED_COLOR = RED>>losses[0]<<shifts[0] |
96
GREEN>>losses[1]<<shifts[1] | BLUE>>losses[2]<<shifts[2]
97
RED = PACKED_COLOR & masks[0] >> shifts[0] << losses[0]
98
GREEN = PACKED_COLOR & masks[1] >> shifts[1] << losses[1]
99
BLUE = PACKED_COLOR & masks[2] >> shifts[2] << losses[2]
100
There is also an alpha channel for some Surfaces.
105
<tr><td><a href=#blit>blit</a></td><td> -
106
copy a one Surface to another.</td></tr>
109
<tr><td><a href=#convert>convert</a></td><td> -
110
new copy of surface with different format</td></tr>
113
<tr><td><a href=#convert_alpha>convert_alpha</a></td><td> -
114
new copy of surface with different format and per pixel alpha</td></tr>
117
<tr><td><a href=#fill>fill</a></td><td> -
118
fill areas of a Surface</td></tr>
121
<tr><td><a href=#get_alpha>get_alpha</a></td><td> -
122
query alpha information</td></tr>
125
<tr><td><a href=#get_at>get_at</a></td><td> -
126
get a pixel color</td></tr>
129
<tr><td><a href=#get_bitsize>get_bitsize</a></td><td> -
130
query size of pixel</td></tr>
133
<tr><td><a href=#get_bytesize>get_bytesize</a></td><td> -
134
query size of pixel</td></tr>
137
<tr><td><a href=#get_clip>get_clip</a></td><td> -
138
query the clipping area</td></tr>
141
<tr><td><a href=#get_colorkey>get_colorkey</a></td><td> -
142
query colorkey</td></tr>
145
<tr><td><a href=#get_flags>get_flags</a></td><td> -
146
query the surface flags</td></tr>
149
<tr><td><a href=#get_height>get_height</a></td><td> -
150
query the surface height</td></tr>
153
<tr><td><a href=#get_locked>get_locked</a></td><td> -
154
check if the surface needs locking</td></tr>
157
<tr><td><a href=#get_losses>get_losses</a></td><td> -
158
get mapping losses for each colorplane</td></tr>
161
<tr><td><a href=#get_masks>get_masks</a></td><td> -
162
get mapping bitmasks for each colorplane</td></tr>
165
<tr><td><a href=#get_palette>get_palette</a></td><td> -
166
get the palette</td></tr>
169
<tr><td><a href=#get_palette_at>get_palette_at</a></td><td> -
170
get a palette entry</td></tr>
173
<tr><td><a href=#get_pitch>get_pitch</a></td><td> -
174
query the surface pitch</td></tr>
177
<tr><td><a href=#get_rect>get_rect</a></td><td> -
178
get a rectangle covering the entire surface</td></tr>
181
<tr><td><a href=#get_shifts>get_shifts</a></td><td> -
185
<tr><td><a href=#get_size>get_size</a></td><td> -
186
query the surface size</td></tr>
189
<tr><td><a href=#get_width>get_width</a></td><td> -
190
query the surface width</td></tr>
193
<tr><td><a href=#lock>lock</a></td><td> -
194
locks Surface for pixel access</td></tr>
197
<tr><td><a href=#map_rgb>map_rgb</a></td><td> -
198
convert RGB into a mapped color</td></tr>
201
<tr><td><a href=#mustlock>mustlock</a></td><td> -
202
check if the surface needs locking</td></tr>
205
<tr><td><a href=#set_alpha>set_alpha</a></td><td> -
206
change alpha information</td></tr>
209
<tr><td><a href=#set_at>set_at</a></td><td> -
210
set pixel at given position</td></tr>
213
<tr><td><a href=#set_clip>set_clip</a></td><td> -
214
assign destination clipping rectangle</td></tr>
217
<tr><td><a href=#set_colorkey>set_colorkey</a></td><td> -
218
change colorkey information</td></tr>
221
<tr><td><a href=#set_palette>set_palette</a></td><td> -
222
set the palette</td></tr>
225
<tr><td><a href=#set_palette_at>set_palette_at</a></td><td> -
226
set a palette entry</td></tr>
229
<tr><td><a href=#subsurface>subsurface</a></td><td> -
230
create a new surface that shares pixel data</td></tr>
233
<tr><td><a href=#unlock>unlock</a></td><td> -
234
locks Surface for pixel access</td></tr>
237
<tr><td><a href=#unmap_rgb>unmap_rgb</a></td><td> -
238
convert mapped color into RGB</td></tr>
245
<a name=blit><font size=+2><b>blit
246
</b></font><br><font size=+1><tt>
247
Surface.blit(source, destpos, [sourcerect]) -> Rect
249
The blitting will transfer one surface to another. It will
250
respect any special modes like colorkeying and alpha. If hardware
251
support is available, it will be used. The given source is the
252
Surface to copy from. The destoffset is a 2-number-sequence that
253
specifies where on the destination Surface the blit happens (see below).
254
When sourcerect isn't supplied, the blit will copy the
255
entire source surface. If you would like to copy only a portion
256
of the source, use the sourcerect argument to control
259
The blit is subject to be clipped by the active clipping
260
rectangle. The return value contains the actual area blitted.
262
As a shortcut, the destination position can be passed as a
263
rectangle. If a rectangle is given, the blit will use the topleft
264
corner of the rectangle as the blit destination position. The
265
rectangle sizes will be ignored.
268
Note that blitting surfaces with alpha onto 8bit destinations will
269
not use the surface alpha values.
273
<a name=convert><font size=+2><b>convert
274
</b></font><br><font size=+1><tt>
275
Surface.convert([src_surface] OR depth, [flags] OR masks) -> Surface
277
Creates a new copy of the surface with the desired pixel format.
278
Surfaces with the same pixel format will blit much faster than
279
those with mixed formats. The pixel format of the new surface
280
will match the format given as the argument. If no surface is
281
given, the new surface will have the same pixel format as the
284
convert() will also accept bitsize or mask arguments like the
285
Surface() constructor function. Either pass an integer bitsize
286
or a sequence of color masks to specify the format of surface
287
you would like to convert to. When used this way you may also
288
pass an optional flags argument (whew).
291
<a name=convert_alpha><font size=+2><b>convert_alpha
292
</b></font><br><font size=+1><tt>
293
Surface.convert_alpha([src_surface]) -> Surface
295
Creates a new copy of the surface with the desired pixel format.
296
The new surface will be in a format suited for quick blitting to
297
the given format with per pixel alpha. If no surface is given,
298
the new surface will be optimized for blittint to the current
301
Unlike the <a href=#convert>convert()</a> method, the pixel format for the new image
302
will not be exactly the same as the requested source, but it will
303
be optimized for fast alpha blitting to the destination.
306
<a name=fill><font size=+2><b>fill
307
</b></font><br><font size=+1><tt>
308
Surface.fill(color, [rectstyle])) -> Rect
310
Fills the specified area of the Surface with the mapped color
311
value. If no destination rectangle is supplied, it will fill the
314
The color argument can be a RGBA sequence or a mapped color integer.
316
The fill is subject to be clipped by the active clipping
317
rectangle. The return value contains the actual area filled.
320
<a name=get_alpha><font size=+2><b>get_alpha
321
</b></font><br><font size=+1><tt>
322
Surface.get_alpha() -> alpha
324
Returns the current alpha value for the Surface. If transparency
325
is disabled for the Surface, it returns None.
328
<a name=get_at><font size=+2><b>get_at
329
</b></font><br><font size=+1><tt>
330
Surface.get_at(position) -> RGBA
332
Returns the RGB color values at a given pixel. If the
333
Surface has no per-pixel alpha, the alpha will be 255 (opaque).
335
This function will need to temporarily lock the surface.
338
<a name=get_bitsize><font size=+2><b>get_bitsize
339
</b></font><br><font size=+1><tt>
340
Surface.get_bitsize() -> int
342
Returns the number of bits used to represent each pixel. This
343
value may not exactly fill the number of bytes used per pixel.
344
For example a 15 bit Surface still requires a full 2 bytes.
347
<a name=get_bytesize><font size=+2><b>get_bytesize
348
</b></font><br><font size=+1><tt>
349
Surface.get_bytesize() -> int
351
Returns the number of bytes used to store each pixel.
354
<a name=get_clip><font size=+2><b>get_clip
355
</b></font><br><font size=+1><tt>
356
Surface.get_clip() -> rect
358
Returns the current destination clipping area being used by the
359
Surface. If the clipping area is not set, it will return a
360
rectangle containing the full Surface area.
363
<a name=get_colorkey><font size=+2><b>get_colorkey
364
</b></font><br><font size=+1><tt>
365
Surface.get_colorkey() -> RGBA
367
Returns the current mapped color value being used for
368
colorkeying. If colorkeying is not enabled for this surface, it
372
<a name=get_flags><font size=+2><b>get_flags
373
</b></font><br><font size=+1><tt>
374
Surface.get_flags() -> flags
376
Returns the current state flags for the surface.
379
<a name=get_height><font size=+2><b>get_height
380
</b></font><br><font size=+1><tt>
381
Surface.get_height() -> height
383
Returns the height of the Surface.
386
<a name=get_locked><font size=+2><b>get_locked
387
</b></font><br><font size=+1><tt>
388
Surface.get_locked() -> bool
390
Returns true if the surface is currently locked.
393
<a name=get_losses><font size=+2><b>get_losses
394
</b></font><br><font size=+1><tt>
395
Surface.get_losses() -> redloss, greenloss, blueloss, alphaloss
397
Returns the bitloss for each color plane. The loss is the number
398
of bits removed for each colorplane from a full 8 bits of
399
resolution. A value of 8 usually indicates that colorplane is not
400
used (like the alpha)
403
<a name=get_masks><font size=+2><b>get_masks
404
</b></font><br><font size=+1><tt>
405
Surface.get_masks() -> redmask, greenmask, bluemask, alphamask
407
Returns the bitmasks for each color plane. The bitmask is used to
408
isolate each colorplane value from a mapped color value. A value
409
of zero means that colorplane is not used (like alpha)
412
<a name=get_palette><font size=+2><b>get_palette
413
</b></font><br><font size=+1><tt>
414
Surface.get_palette() -> [[r, g, b], ...]
416
This will return the an array of all the color indexes in the
420
<a name=get_palette_at><font size=+2><b>get_palette_at
421
</b></font><br><font size=+1><tt>
422
Surface.get_palette_at(index) -> r, g, b
424
This will retreive an individual color entry from the Surface's
428
<a name=get_pitch><font size=+2><b>get_pitch
429
</b></font><br><font size=+1><tt>
430
Surface.get_pitch() -> pitch
432
The surface pitch is the number of bytes used in each
433
scanline. This function should rarely needed, mainly for
434
any special-case debugging.
437
<a name=get_rect><font size=+2><b>get_rect
438
</b></font><br><font size=+1><tt>
439
Surface.get_rect() -> rect
441
Returns a new rectangle covering the entire surface.
442
This rectangle will always start at 0, 0 with a width.
443
and height the same size as the image.
446
<a name=get_shifts><font size=+2><b>get_shifts
447
</b></font><br><font size=+1><tt>
448
Surface.get_shifts() -> redshift, greenshift, blueshift,
451
Returns the bitshifts used for each color plane. The shift is
452
determine how many bits left-shifted a colorplane value is in a
456
<a name=get_size><font size=+2><b>get_size
457
</b></font><br><font size=+1><tt>
458
Surface.get_size() -> x, y
460
Returns the width and height of the Surface.
463
<a name=get_width><font size=+2><b>get_width
464
</b></font><br><font size=+1><tt>
465
Surface.get_width() -> width
467
Returns the width of the Surface.
470
<a name=lock><font size=+2><b>lock
471
</b></font><br><font size=+1><tt>
472
Surface.lock() -> None
474
On accelerated surfaces, it is usually required to lock the
475
surface before you can access the pixel values. To be safe, it is
476
always a good idea to lock the surface before entering a block of
477
code that changes or accesses the pixel values. The surface must
478
not be locked when performing other pygame functions on it like
481
You can doublecheck to really make sure a lock is needed by
482
calling the <a href=#mustlock>mustlock()</a> member. This should not be needed, since
483
it is usually recommended to lock anyways and work with all
484
surface types. If the surface does not need to be locked, the
485
operation will return quickly with minute overhead.
487
On some platforms a necessary lock can shut off some parts of the
488
system. This is not a problem unless you leave surfaces locked
489
for long periouds of time. Only keep the surface locked when you
490
need the pixel access. At the same time, it is not a good too
491
repeatedly lock and unlock the surface inside tight loops. It is
492
fine to leave the surface locked while needed, just don't be
496
<a name=map_rgb><font size=+2><b>map_rgb
497
</b></font><br><font size=+1><tt>
498
Surface.map_rgb(RGBA) -> int
500
Uses the Surface format to convert RGBA into a mapped color value.
502
This function is not as needed as normal C code using SDL. The pygame
503
functions do not used mapped colors, so there is no need to map them.
506
<a name=mustlock><font size=+2><b>mustlock
507
</b></font><br><font size=+1><tt>
508
Surface.mustlock() -> bool
510
Returns true if the surface really does need locking to gain
511
pixel access. Usually the overhead of checking before locking
512
outweight the overhead of just locking any surface before access.
515
<a name=set_alpha><font size=+2><b>set_alpha
516
</b></font><br><font size=+1><tt>
517
Surface.set_alpha([alpha, [flags]]) -> None
519
Set the overall transparency for the surface. If no alpha is
520
passed, alpha blending is disabled for the surface. An alpha of 0
521
is fully transparent, an alpha of 255 is fully opaque. If no
522
arguments or None is passed, this will disable the surface alpha.
524
If your surface has a pixel alpha channel, it will override the
525
overall surface transparency. You'll need to change the actual
526
pixel transparency to make changes.
528
If your image also has pixel alpha values, will be used repeatedly, you
529
will probably want to pass the RLEACCEL flag to the call. This
530
will take a short time to compile your surface, and increase the
534
<a name=set_at><font size=+2><b>set_at
535
</b></font><br><font size=+1><tt>
536
Surface.set_at(position, RGBA) -> None
538
Assigns color to the image at the give position. Color can be a
539
RGBA sequence or a mapped color integer.
541
In some situations just using the <a href=#fill>fill()</a> function with a one-pixel
542
sized rectangle will be quicker. Also the fill function does not
543
require the surface to be locked.
545
This function will need to temporarily lock the surface.
548
<a name=set_clip><font size=+2><b>set_clip
549
</b></font><br><font size=+1><tt>
550
Surface.set_clip([rectstyle]) -> None
552
Assigns the destination clipping rectangle for the Surface. When
553
blit or fill operations are performed on the Surface, they are
554
restricted to the inside of the clipping rectangle. If no
555
rectangle is passed, the clipping region is set to the entire
556
Surface area. The rectangle you pass will be clipped to the area of
560
<a name=set_colorkey><font size=+2><b>set_colorkey
561
</b></font><br><font size=+1><tt>
562
Surface.set_colorkey([color, [flags]]) -> None
564
Set the colorkey for the surface by passing a mapped color value
565
as the color argument. If no arguments or None is passed,
566
colorkeying will be disabled for this surface.
568
The color argument can be either a RGBA sequence or a mapped integer.
570
If your image is nonchanging and will be used repeatedly, you
571
will probably want to pass the RLEACCEL flag to the call. This
572
will take a short time to compile your surface, and increase the
576
<a name=set_palette><font size=+2><b>set_palette
577
</b></font><br><font size=+1><tt>
578
Surface.set_palette([[r, g, b], ...]) -> None
580
This will replace the entire palette with color
581
information you provide.
583
You can pass an incomplete list of RGB values, and
584
this will only change the first colors in the palette.
587
<a name=set_palette_at><font size=+2><b>set_palette_at
588
</b></font><br><font size=+1><tt>
589
Surface.set_palette_at(index, [r, g, b]) -> None
591
This function sets the palette color at a specific entry.
594
<a name=subsurface><font size=+2><b>subsurface
595
</b></font><br><font size=+1><tt>
596
Surface.subsurface(rectstyle) -> Surface
598
Creates a new surface that shares pixel data of the given surface.
599
Note that only the pixel data is shared. Things like clipping rectangles
600
and colorkeys will be unique for the new surface.
602
The new subsurface will inherit the palette, colorkey, and surface alpha
603
values from the base image.
605
You should not use the RLEACCEL flag for parent surfaces of subsurfaces,
606
for the most part it will work, but it will cause a lot of extra work,
607
every time you change the subsurface, you must decode and recode the
608
RLEACCEL data for the parent surface.
610
As for using RLEACCEL with the subsurfaces, that will work as you'd
611
expect, but changes the the parent Surface will not always take effect
615
<a name=unlock><font size=+2><b>unlock
616
</b></font><br><font size=+1><tt>
617
Surface.unlock() -> None
619
After a surface has been locked, you will need to unlock it when
622
You can doublecheck to really make sure a lock is needed by
623
calling the <a href=#mustlock>mustlock()</a> member. This should not be needed, since
624
it is usually recommended to lock anyways and work with all
625
surface types. If the surface does not need to be locked, the
626
operation will return quickly with minute overhead.
629
<a name=unmap_rgb><font size=+2><b>unmap_rgb
630
</b></font><br><font size=+1><tt>
631
Surface.unmap_rgb(color) -> RGBA
633
This function returns the RGBA components for a mapped color
634
value. If Surface has no per-pixel alpha, alpha will be 255 (opaque).
636
This function is not as needed as normal C code using SDL. The pygame
637
functions do not used mapped colors, so there is no need to unmap them.