Source

pygame / docs / ref / pixelarray.html


<html>
<title>pixelarray - Pygame Documentation</title>
<body bgcolor=#aaeebb text=#000000 link=#331111 vlink=#331111>


<table cellpadding=0 cellspacing=0 border=0 style='border: 3px solid black;' width='100%'>
<tr>
<td bgcolor='#c2fc20' style='padding: 6px;' align=center valign=center><a href='http://www.pygame.org/'><img src='../pygame_tiny.gif' border=0 width=200 height=60></a><br><b>pygame documentation</b></td>
<td bgcolor='#6aee28' style='border-left: 3px solid black; padding: 6px;' align=center valign=center>
	||&nbsp;
	<a href=http://www.pygame.org>Pygame Home</a> &nbsp;||&nbsp;
	<a href=../index.html>Help Contents</a> &nbsp;||
	<a href=index.html>Reference Index</a> &nbsp;||
	<br>&nbsp;<br>
	
<a href="camera.html">Camera</a>&nbsp;||&nbsp;
<a href="cdrom.html">Cdrom</a>&nbsp;||&nbsp;
<a href="color.html">Color</a>&nbsp;||&nbsp;
<a href="cursors.html">Cursors</a>&nbsp;||&nbsp;
<a href="display.html">Display</a>&nbsp;||&nbsp;
<a href="draw.html">Draw</a>&nbsp;||&nbsp;
<a href="event.html">Event</a>&nbsp;||&nbsp;
<a href="examples.html">Examples</a>&nbsp;||&nbsp;
<a href="font.html">Font</a>&nbsp;||&nbsp;
<a href="freetype.html">Freetype</a>&nbsp;||&nbsp;
<a href="gfxdraw.html">Gfxdraw</a>&nbsp;||&nbsp;
<a href="image.html">Image</a>&nbsp;||&nbsp;
<a href="joystick.html">Joystick</a>&nbsp;||&nbsp;
<a href="key.html">Key</a>&nbsp;||&nbsp;
<a href="locals.html">Locals</a>&nbsp;||&nbsp;
<a href="mask.html">Mask</a>&nbsp;||&nbsp;
<a href="midi.html">Midi</a>&nbsp;||&nbsp;
<a href="mixer.html">Mixer</a>&nbsp;||&nbsp;
<a href="mouse.html">Mouse</a>&nbsp;||&nbsp;
<a href="movie.html">Movie</a>&nbsp;||&nbsp;
<a href="music.html">Music</a>&nbsp;||&nbsp;
<a href="overlay.html">Overlay</a>&nbsp;||&nbsp;
<a href="pixelarray.html">Pixelarray</a>&nbsp;||&nbsp;
<a href="pygame.html">Pygame</a>&nbsp;||&nbsp;
<a href="rect.html">Rect</a>&nbsp;||&nbsp;
<a href="scrap.html">Scrap</a>&nbsp;||&nbsp;
<a href="sndarray.html">Sndarray</a>&nbsp;||&nbsp;
<a href="sprite.html">Sprite</a>&nbsp;||&nbsp;
<a href="surface.html">Surface</a>&nbsp;||&nbsp;
<a href="surfarray.html">Surfarray</a>&nbsp;||&nbsp;
<a href="tests.html">Tests</a>&nbsp;||&nbsp;
<a href="time.html">Time</a>&nbsp;||&nbsp;
<a href="transform.html">Transform</a>
</td></tr></table>
<br>


<a name="pygame.PixelArray">
<big><b>pygame.PixelArray</big></b><br><ul>
  <i>pygame object for direct pixel access of surfaces</i><br>
  <tt>pygame.PixelArray(Surface): return PixelArray</tt><br>
<ul><small><table>
  <tr><td><a href="pixelarray.html#PixelArray.surface">PixelArray.surface</a> - <font size=-1>Gets the Surface the PixelArray uses.</font></td><td>Gets the Surface the PixelArray uses.</td></tr>
  <tr><td><a href="pixelarray.html#PixelArray.make_surface">PixelArray.make_surface</a> - <font size=-1>Creates a new Surface from the current PixelArray.</font></td><td>Creates a new Surface from the current PixelArray.</td></tr>
  <tr><td><a href="pixelarray.html#PixelArray.replace">PixelArray.replace</a> - <font size=-1>Replaces the passed color in the PixelArray with another one.</font></td><td>Replaces the passed color in the PixelArray with another one.</td></tr>
  <tr><td><a href="pixelarray.html#PixelArray.extract">PixelArray.extract</a> - <font size=-1>Extracts the passed color from the PixelArray.</font></td><td>Extracts the passed color from the PixelArray.</td></tr>
  <tr><td><a href="pixelarray.html#PixelArray.compare">PixelArray.compare</a> - <font size=-1>Compares the PixelArray with another one.</font></td><td>Compares the PixelArray with another one.</td></tr>
</table></small></ul>
<p>The PixelArray wraps up a Surface and provides a direct <tt>2D</tt> array access to its pixels using the surface its rows as first and its columns as second axis. It supports slicing, row and pixel manipluation, slicing and slice assignments while inplace operations such as addition, subtraction, multiplication, division and so forth are not allowed. </p>
<p>While it is possible to assign both, integer color values and <tt>RGB(A)</tt> color tuples, the PixelArray will only use integers for the color representation. Thus, checking for certain colors has to be done using the <tt><a href="surface.html#Surface.map_rgb">Surface.map_rgb</a> - <font size=-1>convert a color into a mapped color value</font></tt> method of the surface, the PixelArray was created for. </p>
<pre>  pxarray = pygame.PixelArray (surface)
  # Check, if the first pixel at the topleft corner is blue
  if pxarray[0][0] == surface.map_rgb ((0, 0, 255)):
      ...
</pre><p>Pixels can be manipulated using integer values or color tuples. </p>
<pre>  pxarray[x][y] = 0xFF00FF
  pxarray[x][y] = (255, 0, 255)
</pre><p>If you operate on a slice, you also can use arbitrary sequences or other PixelArray objects to modify the pixels. They have to match the size of the PixelArray however. </p>
<pre>  pxarray[a:b] = 0xFF00FF                   # set all pixels to 0xFF00FF
  pxarray[a:b] = (0xFF00FF, 0xAACCEE, ... ) # first pixel = 0xFF00FF,
                                            # second pixel  = 0xAACCEE, ...
  pxarray[a:b] = ((255, 0, 255), (170, 204, 238), ...) # same as above
  pxarray[a:b] = ((255, 0, 255), 0xAACCEE, ...)        # same as above
  pxarray[a:b] = otherarray[x:y]            # slice sizes must match
</pre><p>Note, that something like </p>
<pre>  pxarray[2:4][3:5] = ...
</pre><p>will not cause a rectangular manipulation. Instead it will be first sliced to a two-column array, which then shall be sliced by columns once more, which will fail due an IndexError. This is caused by the slicing mechanisms in python and an absolutely correct behaviour. Create a single columned slice first, which you can manipulate then: </p>
<pre>  pxarray[2][3:5] = ...
  pxarray[3][3:5] = ...
</pre><p>If you want to make a rectangular manipulation or create a view of a part of the PixelArray, you also can use the subscript abilities. You can easily create different view by creating 'subarrays' using the subscripts. </p>
<pre>  # Create some new PixelArray objects providing a different view
  # of the original array/surface.
  newarray = pxarray[2:4,3:5]
  otherarray = pxarray[::2,::2]
</pre><p>Subscripts also can be used to do fast rectangular pixel manipulations instead of iterating over the x or y axis as above. </p>
<pre>  pxarray[::2,:] = (0, 0, 0)                # Make each second column black.
</pre><p>During its lifetime, the PixelArray locks the surface, thus you explicitly have to delete it once its not used anymore and the surface should perform operations in the same scope. </p>
<p>New in pygame <tt>1.8</tt>. Subscript support is new in pygame <tt>1.8.1</tt>. </p>
<!--COMMENTS:pygame.PixelArray--> &nbsp;<br> 


<a name="PixelArray.surface">
<big><b>PixelArray.surface</big></b><br><ul>
  <i>Gets the Surface the PixelArray uses.</i><br>
  <tt>PixelArray.surface: Return Surface</tt><br>
<p>The Surface, the PixelArray was created for. </p>
<!--COMMENTS:PixelArray.surface--> &nbsp;<br> 
<br></ul>


<a name="PixelArray.make_surface">
<big><b>PixelArray.make_surface</big></b><br><ul>
  <i>Creates a new Surface from the current PixelArray.</i><br>
  <tt>PixelArray.make_surface (): Return Surface</tt><br>
<p>Creates a new Surface from the current PixelArray. Depending on the current PixelArray the size, pixel order etc. will be different from the original Surface. </p>
<pre>  # Create a new surface flipped around the vertical axis.
  sf = pxarray[:,::-1].make_surface ()
</pre><p>New in pygame <tt>1.8.1</tt>. </p>
<!--COMMENTS:PixelArray.make_surface--> &nbsp;<br> 
<br></ul>


<a name="PixelArray.replace">
<big><b>PixelArray.replace</big></b><br><ul>
  <i>Replaces the passed color in the PixelArray with another one.</i><br>
  <tt>PixelArray.replace (color, repcolor, distance=0, weights=(0.299, 0.587, 0.114)): Return None</tt><br>
<p>Replaces the pixels with the passed color in the PixelArray by changing them them to the passed replacement color. </p>
<p>It uses a simple weighted euclidian distance formula to calculate the distance between the colors. The distance space ranges from <tt>0.0</tt> to <tt>1.0</tt> and is used as threshold for the color detection. This causes the replacement to take pixels with a similar, but not exactly identical color, into account as well. </p>
<p>This is an in place operation that directly affects the pixels of the PixelArray. </p>
<p>New in pygame <tt>1.8.1</tt>. </p>
<!--COMMENTS:PixelArray.replace--> &nbsp;<br> 
<br></ul>


<a name="PixelArray.extract">
<big><b>PixelArray.extract</big></b><br><ul>
  <i>Extracts the passed color from the PixelArray.</i><br>
  <tt>PixelArray.extract (color, distance=0, weights=(0.299, 0.587, 0.114)): Return PixelArray</tt><br>
<p>Extracts the passed color by changing all matching pixels to white, while non-matching pixels are changed to black. This returns a new PixelArray with the black/white color mask. </p>
<p>It uses a simple weighted euclidian distance formula to calculate the distance between the colors. The distance space ranges from <tt>0.0</tt> to <tt>1.0</tt> and is used as threshold for the color detection. This causes the extraction to take pixels with a similar, but not exactly identical color, into account as well. </p>
<p>New in pygame <tt>1.8.1</tt>. </p>
<!--COMMENTS:PixelArray.extract--> &nbsp;<br> 
<br></ul>


<a name="PixelArray.compare">
<big><b>PixelArray.compare</big></b><br><ul>
  <i>Compares the PixelArray with another one.</i><br>
  <tt>PixelArray.compare (array, distance=0, weights=(0.299, 0.587, 0.114)): Return PixelArray</tt><br>
<p>Compares the contents of the PixelArray with those from the passed PixelArray. It returns a new PixelArray with a black/white color mask that indicates the differences (white) of both arrays. Both PixelArray objects must have indentical bit depths and dimensions. </p>
<p>It uses a simple weighted euclidian distance formula to calculate the distance between the colors. The distance space ranges from <tt>0.0</tt> to <tt>1.0</tt> and is used as threshold for the color detection. This causes the comparision to mark pixels with a similar, but not exactly identical color, as black. </p>
<p>New in pygame <tt>1.8.1</tt>. </p>
<!--COMMENTS:PixelArray.compare--> &nbsp;<br> 
<br></ul>
<br></ul>

</body></html>