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
|
Image
*****
.. py:module:: microbit
The ``Image`` class is used to create images that can be displayed easily on
the device's LED matrix. Given an image object it's possible to display it via
the ``display`` API::
display.show(Image.HAPPY)
.. image:: image-smile.png
There are four ways in which you can construct an image:
- ``Image()`` - Create a blank 5x5 image
- ``Image(string)`` - Create an image by parsing the string, a single character
returns that glyph
- ``Image(width, height)`` - Create a blank image of given size
- ``Image(width, height, buffer)`` - Create an image from the given buffer
Classes
=======
.. py:class::
Image(string)
Image(width=None, height=None, buffer=None)
If ``string`` is used, it has to consist of digits 0-9 arranged into
lines, describing the image, for example::
image = Image("90009:"
"09090:"
"00900:"
"09090:"
"90009")
will create a 5×5 image of an X. The end of a line is indicated by a colon.
It's also possible to use a newline (\n) to indicate the end of a line
like this::
image = Image("90009\n"
"09090\n"
"00900\n"
"09090\n"
"90009")
The other form creates an empty image with ``width`` columns and
``height`` rows. Optionally ``buffer`` can be an array of
``width``×``height`` integers in range 0-9 to initialize the image::
Image(2, 2, b'\x08\x08\x08\x08')
or::
Image(2, 2, bytearray([9,9,9,9]))
Will create a 2 x 2 pixel image at full brightness.
.. note::
Keyword arguments cannot be passed to ``buffer``.
.. py:method:: width()
Return the number of columns in the image.
.. py:method:: height()
Return the numbers of rows in the image.
.. py:method:: set_pixel(x, y, value)
Set the brightness of the pixel at column ``x`` and row ``y`` to the
``value``, which has to be between 0 (dark) and 9 (bright).
This method will raise an exception when called on any of the built-in
read-only images, like ``Image.HEART``.
.. py:method:: get_pixel(x, y)
Return the brightness of pixel at column ``x`` and row ``y`` as an
integer between 0 and 9.
.. py:method:: shift_left(n)
Return a new image created by shifting the picture left by ``n``
columns.
.. py:method:: shift_right(n)
Same as ``image.shift_left(-n)``.
.. py:method:: shift_up(n)
Return a new image created by shifting the picture up by ``n`` rows.
.. py:method:: shift_down(n)
Same as ``image.shift_up(-n)``.
.. py:method:: crop(x, y, w, h)
Return a new image by cropping the picture to a width of ``w`` and a
height of ``h``, starting with the pixel at column ``x`` and row ``y``.
.. py:method:: copy()
Return an exact copy of the image.
.. py:method:: invert()
Return a new image by inverting the brightness of the pixels in the
source image.
.. py:method:: fill(value)
Set the brightness of all the pixels in the image to the
``value``, which has to be between 0 (dark) and 9 (bright).
This method will raise an exception when called on any of the built-in
read-only images, like ``Image.HEART``.
.. py:method:: blit(src, x, y, w, h, xdest=0, ydest=0)
Copy the rectangle defined by ``x``, ``y``, ``w``, ``h`` from the image ``src`` into
this image at ``xdest``, ``ydest``.
Areas in the source rectangle, but outside the source image are treated as having a value of 0.
``shift_left()``, ``shift_right()``, ``shift_up()``, ``shift_down()`` and ``crop()``
can are all implemented by using ``blit()``.
For example, img.crop(x, y, w, h) can be implemented as::
def crop(self, x, y, w, h):
res = Image(w, h)
res.blit(self, x, y, w, h)
return res
Attributes
==========
The ``Image`` class also has the following built-in instances of itself
included as its attributes (the attribute names indicate what the image
represents):
* ``Image.HEART``
* ``Image.HEART_SMALL``
* ``Image.HAPPY``
* ``Image.SMILE``
* ``Image.SAD``
* ``Image.CONFUSED``
* ``Image.ANGRY``
* ``Image.ASLEEP``
* ``Image.SURPRISED``
* ``Image.SILLY``
* ``Image.FABULOUS``
* ``Image.MEH``
* ``Image.YES``
* ``Image.NO``
* ``Image.CLOCK12``, ``Image.CLOCK11``, ``Image.CLOCK10``, ``Image.CLOCK9``,
``Image.CLOCK8``, ``Image.CLOCK7``, ``Image.CLOCK6``, ``Image.CLOCK5``,
``Image.CLOCK4``, ``Image.CLOCK3``, ``Image.CLOCK2``, ``Image.CLOCK1``
* ``Image.ARROW_N``, ``Image.ARROW_NE``, ``Image.ARROW_E``,
``Image.ARROW_SE``, ``Image.ARROW_S``, ``Image.ARROW_SW``,
``Image.ARROW_W``, ``Image.ARROW_NW``
* ``Image.TRIANGLE``
* ``Image.TRIANGLE_LEFT``
* ``Image.CHESSBOARD``
* ``Image.DIAMOND``
* ``Image.DIAMOND_SMALL``
* ``Image.SQUARE``
* ``Image.SQUARE_SMALL``
* ``Image.RABBIT``
* ``Image.COW``
* ``Image.MUSIC_CROTCHET``
* ``Image.MUSIC_QUAVER``
* ``Image.MUSIC_QUAVERS``
* ``Image.PITCHFORK``
* ``Image.XMAS``
* ``Image.PACMAN``
* ``Image.TARGET``
* ``Image.TSHIRT``
* ``Image.ROLLERSKATE``
* ``Image.DUCK``
* ``Image.HOUSE``
* ``Image.TORTOISE``
* ``Image.BUTTERFLY``
* ``Image.STICKFIGURE``
* ``Image.GHOST``
* ``Image.SWORD``
* ``Image.GIRAFFE``
* ``Image.SKULL``
* ``Image.UMBRELLA``
* ``Image.SNAKE``
Finally, related collections of images have been grouped together::
* ``Image.ALL_CLOCKS``
* ``Image.ALL_ARROWS``
Operations
==========
.. code::
repr(image)
Get a compact string representation of the image.
.. code::
str(image)
Get a readable string representation of the image.
.. code::
image1 + image2
Create a new image by adding the brightness values from the two images for
each pixel.
.. code::
image * n
Create a new image by multiplying the brightness of each pixel by ``n``.
|