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 296 297 298 299 300 301 302 303 304 305 306 307 308
|
.. include:: common.txt
:mod:`pygame.mask`
==================
.. module:: pygame.mask
:synopsis: pygame module for image masks.
| :sl:`pygame module for image masks.`
Useful for fast pixel perfect collision detection. A mask uses 1 bit per-pixel
to store which parts collide.
.. versionadded:: 1.8
Starting from pygame 1.9.5 masks with width or height 0 are supported.
.. function:: from_surface
| :sl:`Returns a Mask from the given surface.`
| :sg:`from_surface(Surface, threshold = 127) -> Mask`
Makes the transparent parts of the Surface not set, and the opaque parts
set.
The alpha of each pixel is checked to see if it is greater than the given
threshold.
If the Surface is color-keyed, then threshold is not used.
.. ## pygame.mask.from_surface ##
.. function:: from_threshold
| :sl:`Creates a mask by thresholding Surfaces`
| :sg:`from_threshold(Surface, color, threshold = (0,0,0,255), othersurface = None, palette_colors = 1) -> Mask`
This is a more-featureful method of getting a Mask from a Surface. If
supplied with only one Surface, all pixels within the threshold of the
supplied color are set in the Mask. If given the optional othersurface, all
pixels in Surface that are within the threshold of the corresponding pixel
in othersurface are set in the Mask.
.. ## pygame.mask.from_threshold ##
.. class:: Mask
| :sl:`pygame object for representing 2D bitmasks`
| :sg:`Mask(size=(width, height)) -> Mask`
| :sg:`Mask(size=(width, height), fill=False) -> Mask`
A ``Mask`` object is used to represent a 2D bitmask. Each bit in
the mask represents a pixel. 1 is used to indicate a set bit and 0 is used
to indicate an unset bit. Set bits in a mask can be used to detect
collisions with other masks and their set bits.
A filled mask has all of its bits set to 1, conversely an unfilled/cleared
mask has all of its bits set to 0. Masks can be created unfilled (default)
or filled by using the ``fill`` parameter. Masks can also be cleared or
filled using the :func:`pygame.mask.Mask.clear()` and
:func:`pygame.mask.Mask.fill()` methods respectively. Individual bits can
be accessed using the :func:`pygame.mask.Mask.get_at()` and
:func:`pygame.mask.Mask.set_at()` methods.
:param size: the dimensions of the mask (width and height)
:type size: tuple(int, int) or list[int, int]
:param bool fill: create mask unfilled (``False`` - default) or filled
(``True``)
:rtype: Mask
.. versionadded:: 1.9.5 Named parameter ``size`` (previously it was an
unnamed positional parameter) and the optional keyword parameter
``fill``.
.. method:: get_size
| :sl:`Returns the size of the mask.`
| :sg:`get_size() -> width,height`
.. ## Mask.get_size ##
.. method:: get_at
| :sl:`Returns nonzero if the bit at (x,y) is set.`
| :sg:`get_at((x,y)) -> int`
Coordinates start at (0,0) is top left - just like Surfaces.
.. ## Mask.get_at ##
.. method:: set_at
| :sl:`Sets the position in the mask given by x and y.`
| :sg:`set_at((x,y),value) -> None`
.. ## Mask.set_at ##
.. method:: overlap
| :sl:`Returns the point of intersection if the masks overlap with the given offset - or None if it does not overlap.`
| :sg:`overlap(othermask, offset) -> x,y`
The overlap tests uses the following offsets (which may be negative):
::
+----+----------..
|A | yoffset
| +-+----------..
+--|B
|xoffset
| |
: :
.. ## Mask.overlap ##
.. method:: overlap_area
| :sl:`Returns the number of overlapping 'pixels'.`
| :sg:`overlap_area(othermask, offset) -> numpixels`
You can see how many pixels overlap with the other mask given. This can
be used to see in which direction things collide, or to see how much the
two masks collide. An approximate collision normal can be found by
calculating the gradient of the overlap area through the finite
difference.
::
dx = Mask.overlap_area(othermask,(x+1,y)) - Mask.overlap_area(othermask,(x-1,y))
dy = Mask.overlap_area(othermask,(x,y+1)) - Mask.overlap_area(othermask,(x,y-1))
.. ## Mask.overlap_area ##
.. method:: overlap_mask
| :sl:`Returns a mask of the overlapping pixels`
| :sg:`overlap_mask(othermask, offset) -> Mask`
Returns a Mask the size of the original Mask containing only the
overlapping pixels between Mask and othermask.
.. ## Mask.overlap_mask ##
.. method:: fill
| :sl:`Sets all bits to 1`
| :sg:`fill() -> None`
Sets all bits in a Mask to 1.
.. ## Mask.fill ##
.. method:: clear
| :sl:`Sets all bits to 0`
| :sg:`clear() -> None`
Sets all bits in a Mask to 0.
.. ## Mask.clear ##
.. method:: invert
| :sl:`Flips the bits in a Mask`
| :sg:`invert() -> None`
Flips all of the bits in a Mask, so that the set pixels turn to unset
pixels and the unset pixels turn to set pixels.
.. ## Mask.invert ##
.. method:: scale
| :sl:`Resizes a mask`
| :sg:`scale((x, y)) -> Mask`
Returns a new Mask of the Mask scaled to the requested size.
.. ## Mask.scale ##
.. method:: draw
| :sl:`Draws a mask onto another`
| :sg:`draw(othermask, offset) -> None`
Performs a bitwise ``OR``, drawing othermask onto Mask.
.. ## Mask.draw ##
.. method:: erase
| :sl:`Erases a mask from another`
| :sg:`erase(othermask, offset) -> None`
Erases all pixels set in othermask from Mask.
.. ## Mask.erase ##
.. method:: count
| :sl:`Returns the number of set pixels`
| :sg:`count() -> pixels`
Returns the number of set pixels in the Mask.
.. ## Mask.count ##
.. method:: centroid
| :sl:`Returns the centroid of the pixels in a Mask`
| :sg:`centroid() -> (x, y)`
Finds the centroid, the center of pixel mass, of a Mask. Returns a
coordinate tuple for the centroid of the Mask. In the event the Mask is
empty, it will return (0,0).
.. ## Mask.centroid ##
.. method:: angle
| :sl:`Returns the orientation of the pixels`
| :sg:`angle() -> theta`
Finds the approximate orientation of the pixels in the image from -90 to
90 degrees. This works best if performed on one connected component of
pixels. It will return 0.0 on an empty Mask.
.. ## Mask.angle ##
.. method:: outline
| :sl:`list of points outlining an object`
| :sg:`outline(every = 1) -> [(x,y), (x,y) ...]`
Returns a list of points of the outline of the first object it comes
across in a Mask. For this to be useful, there should probably only be
one connected component of pixels in the Mask. The every option allows
you to skip pixels in the outline. For example, setting it to 10 would
return a list of every 10th pixel in the outline.
.. ## Mask.outline ##
.. method:: convolve
| :sl:`Return the convolution of self with another mask.`
| :sg:`convolve(othermask) -> Mask`
| :sg:`convolve(othermask, outputmask=None, offset=(0,0)) -> Mask`
Convolve self with the given ``othermask``.
:param Mask othermask: mask to convolve with self
:param outputmask: mask for output, default is None
:type outputmask: Mask or None
:param offset: offset used in convolution of masks, default is (0, 0)
:type offset: tuple(int, int) or list[int, int]
:returns: A mask with the ``(i - offset[0], j - offset[1])`` bit set, if
shifting ``othermask`` (such that its bottom right corner pixel is at
``(i, j)``) causes it to overlap with self.
If an ``outputmask`` is specified, the output is drawn onto it and
it is returned. Otherwise a mask of size ``(MAX(0, width + othermask's
width - 1), MAX(0, height + othermask's height - 1))`` is created and
returned.
:rtype: Mask
.. ## Mask.convolve ##
.. method:: connected_component
| :sl:`Returns a mask of a connected region of pixels.`
| :sg:`connected_component((x,y) = None) -> Mask`
This uses the ``SAUF`` algorithm to find a connected component in the
Mask. It checks 8 point connectivity. By default, it will return the
largest connected component in the image. Optionally, a coordinate pair
of a pixel can be specified, and the connected component containing it
will be returned. In the event the pixel at that location is not set, the
returned Mask will be empty. The Mask returned is the same size as the
original Mask.
.. ## Mask.connected_component ##
.. method:: connected_components
| :sl:`Returns a list of masks of connected regions of pixels.`
| :sg:`connected_components(min = 0) -> [Masks]`
Returns a list of masks of connected regions of pixels. An optional
minimum number of pixels per connected region can be specified to filter
out noise.
.. ## Mask.connected_components ##
.. method:: get_bounding_rects
| :sl:`Returns a list of bounding rects of regions of set pixels.`
| :sg:`get_bounding_rects() -> Rects`
This gets a bounding rect of connected regions of set pixels. A bounding
rect is one for which each of the connected pixels is inside the rect.
.. ## Mask.get_bounding_rects ##
.. ## pygame.mask.Mask ##
.. ## pygame.mask ##
|