File: pixelarray.rst.txt

package info (click to toggle)
pygame 2.1.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,416 kB
  • sloc: ansic: 66,042; python: 46,176; javascript: 9,214; objc: 273; sh: 78; makefile: 56; cpp: 25
file content (295 lines) | stat: -rw-r--r-- 10,207 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
.. include:: common.txt

:class:`pygame.PixelArray`
==========================

.. currentmodule:: pygame

.. class:: PixelArray

   | :sl:`pygame object for direct pixel access of surfaces`
   | :sg:`PixelArray(Surface) -> PixelArray`

   The PixelArray wraps a Surface and provides direct access to the
   surface's pixels. A pixel array can be one or two dimensional.
   A two dimensional array, like its surface, is indexed [column, row].
   Pixel arrays support slicing, both for returning a subarray or
   for assignment. A pixel array sliced on a single column or row
   returns a one dimensional pixel array. Arithmetic and other operations
   are not supported. A pixel array can be safely assigned to itself.
   Finally, pixel arrays export an array struct interface, allowing
   them to interact with :mod:`pygame.pixelcopy` methods and NumPy
   arrays.

   A PixelArray pixel item can be assigned a raw integer values, a
   :class:`pygame.Color` instance, or a (r, g, b[, a]) tuple.

   ::

     pxarray[x, y] = 0xFF00FF
     pxarray[x, y] = pygame.Color(255, 0, 255)
     pxarray[x, y] = (255, 0, 255)

   However, only a pixel's integer value is returned. So, to compare a pixel
   to a particular color the color needs to be first mapped using
   the :meth:`Surface.map_rgb()` method of the Surface object for which the
   PixelArray was created.

   ::

     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)):
         ...

   When assigning to a range of of pixels, a non tuple sequence of colors or
   a PixelArray can be used as the value. For a sequence, the length must
   match the PixelArray width.

   ::

     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

   For PixelArray assignment, if the right hand side array has a row length
   of 1, then the column is broadcast over the target array's rows. An
   array of height 1 is broadcast over the target's columns, and is equivalent
   to assigning a 1D PixelArray.

   Subscript slices can also be used to assign to a rectangular subview of
   the target PixelArray.

   ::

     # Create some new PixelArray objects providing a different view
     # of the original array/surface.
     newarray = pxarray[2:4, 3:5]
     otherarray = pxarray[::2, ::2]

   Subscript slices can also be used to do fast rectangular pixel manipulations
   instead of iterating over the x or y axis. The

   ::

     pxarray[::2, :] = (0, 0, 0)               # Make even columns black.
     pxarray[::2] = (0, 0, 0)                  # Same as [::2, :]

   During its lifetime, the PixelArray locks the surface, thus you explicitly
   have to close() it once its not used any more and the surface should perform
   operations in the same scope. It is best to use it as a context manager
   using the with PixelArray(surf) as pixel_array: style. So it works on pypy too.

   A simple ``:`` slice index for the column can be omitted.

   ::

     pxarray[::2, ...] = (0, 0, 0)             # Same as pxarray[::2, :]
     pxarray[...] = (255, 0, 0)                # Same as pxarray[:]

   A note about PixelArray to PixelArray assignment, for arrays with an
   item size of 3 (created from 24 bit surfaces) pixel values are translated
   from the source to the destinations format. The red, green, and blue
   color elements of each pixel are shifted to match the format of the
   target surface. For all other pixel sizes no such remapping occurs.
   This should change in later pygame releases, where format conversions
   are performed for all pixel sizes. To avoid code breakage when full mapped
   copying is implemented it is suggested PixelArray to PixelArray copies be
   only between surfaces of identical format.

   .. versionadded:: 1.9.4

    - close() method was added. For explicitly cleaning up.
    - being able to use PixelArray as a context manager for cleanup.
    - both of these are useful for when working without reference counting (pypy).

   .. versionadded:: 1.9.2

    - array struct interface
    - transpose method
    - broadcasting for a length 1 dimension

   .. versionchanged:: 1.9.2

    - A 2D PixelArray can have a length 1 dimension.
      Only an integer index on a 2D PixelArray returns a 1D array.
    - For assignment, a tuple can only be a color. Any other sequence type
      is a sequence of colors.


   .. versionadded: 1.8.0
      Subscript support

   .. versionadded: 1.8.1
      Methods :meth:`make_surface`, :meth:`replace`, :meth:`extract`, and
      :meth:`compare`

   .. versionadded: 1.9.2
      Properties :attr:`itemsize`, :attr:`ndim`, :attr:`shape`,
      and :attr:`strides`

   .. versionadded: 1.9.2
      Array struct interface

   .. versionadded: 1.9.4
      Methods :meth:`close`

   .. attribute:: surface

      | :sl:`Gets the Surface the PixelArray uses.`
      | :sg:`surface -> Surface`

      The Surface the PixelArray was created for.

      .. ## PixelArray.surface ##

   .. attribute:: itemsize

      | :sl:`Returns the byte size of a pixel array item`
      | :sg:`itemsize -> int`

      This is the same as :meth:`Surface.get_bytesize` for the
      pixel array's surface.

      .. versionadded:: 1.9.2

   .. attribute:: ndim

      | :sl:`Returns the number of dimensions.`
      | :sg:`ndim -> int`

      A pixel array can be 1 or 2 dimensional.

      .. versionadded:: 1.9.2

   .. attribute:: shape

      | :sl:`Returns the array size.`
      | :sg:`shape -> tuple of int's`

      A tuple or length :attr:`ndim` giving the length of each
      dimension. Analogous to :meth:`Surface.get_size`.

      .. versionadded:: 1.9.2

   .. attribute:: strides

      | :sl:`Returns byte offsets for each array dimension.`
      | :sg:`strides -> tuple of int's`

      A tuple or length :attr:`ndim` byte counts. When a stride is
      multiplied by the corresponding index it gives the offset
      of that index from the start of the array. A stride is negative
      for an array that has is inverted (has a negative step).

      .. versionadded:: 1.9.2

   .. method:: make_surface

      | :sl:`Creates a new Surface from the current PixelArray.`
      | :sg:`make_surface() -> Surface`

      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.

      ::

        # Create a new surface flipped around the vertical axis.
        sf = pxarray[:,::-1].make_surface ()

      .. versionadded:: 1.8.1

      .. ## PixelArray.make_surface ##

   .. method:: replace

      | :sl:`Replaces the passed color in the PixelArray with another one.`
      | :sg:`replace(color, repcolor, distance=0, weights=(0.299, 0.587, 0.114)) -> None`

      Replaces the pixels with the passed color in the PixelArray by changing
      them them to the passed replacement color.

      It uses a simple weighted Euclidean distance formula to calculate the
      distance between the colors. The distance space ranges from 0.0 to 1.0
      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.

      This is an in place operation that directly affects the pixels of the
      PixelArray.

      .. versionadded:: 1.8.1

      .. ## PixelArray.replace ##

   .. method:: extract

      | :sl:`Extracts the passed color from the PixelArray.`
      | :sg:`extract(color, distance=0, weights=(0.299, 0.587, 0.114)) -> PixelArray`

      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.

      It uses a simple weighted Euclidean distance formula to calculate the
      distance between the colors. The distance space ranges from 0.0 to 1.0
      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.

      .. versionadded:: 1.8.1

      .. ## PixelArray.extract ##

   .. method:: compare

      | :sl:`Compares the PixelArray with another one.`
      | :sg:`compare(array, distance=0, weights=(0.299, 0.587, 0.114)) -> PixelArray`

      Compares the contents of the PixelArray with those from the passed in
      PixelArray. It returns a new PixelArray with a black/white color mask
      that indicates the differences (black) of both arrays. Both PixelArray
      objects must have identical bit depths and dimensions.

      It uses a simple weighted Euclidean distance formula to calculate the
      distance between the colors. The distance space ranges from 0.0 to 1.0
      and is used as a threshold for the color detection. This causes the
      comparison to mark pixels with a similar, but not exactly identical
      color, as white.

      .. versionadded:: 1.8.1

      .. ## PixelArray.compare ##

   .. method:: transpose

      | :sl:`Exchanges the x and y axis.`
      | :sg:`transpose() -> PixelArray`

      This method returns a new view of the pixel array with the rows and
      columns swapped. So for a (w, h) sized array a (h, w) slice is returned.
      If an array is one dimensional, then a length 1 x dimension is added,
      resulting in a 2D pixel array.

      .. versionadded:: 1.9.2

      .. ## PixelArray.transpose ##

   .. method:: close

      | :sl:`Closes the PixelArray, and releases Surface lock.`
      | :sg:`transpose() -> PixelArray`

      This method is for explicitly closing the PixelArray, and releasing
      a lock on the Suface.

      .. versionadded:: 1.9.4

      .. ## PixelArray.close ##


   .. ## pygame.PixelArray ##