Image¶
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)
There are four ways in which you can construct an image:
Image()- Create a blank 5x5 imageImage(string)- Create an image by parsing the string, a single character returns that glyphImage(width, height)- Create a blank image of given sizeImage(width, height, buffer)- Create an image from the given buffer
Classes¶
-
class
microbit.Image(string)¶ -
class
microbit.Image(width=None, height=None, buffer=None) If
stringis 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
widthcolumns andheightrows. Optionallybuffercan be an array ofwidth``×``heightintegers 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.-
width()¶ Return the number of columns in the image.
-
height()¶ Return the numbers of rows in the image.
-
set_pixel(x, y, value)¶ Set the brightness of the pixel at column
xand rowyto thevalue, 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.
-
get_pixel(x, y)¶ Return the brightness of pixel at column
xand rowyas an integer between 0 and 9.
-
shift_left(n)¶ Return a new image created by shifting the picture left by
ncolumns.
-
shift_right(n)¶ Same as
image.shift_left(-n).
-
shift_up(n)¶ Return a new image created by shifting the picture up by
nrows.
-
shift_down(n)¶ Same as
image.shift_up(-n).
-
crop(x, y, w, h)¶ Return a new image by cropping the picture to a width of
wand a height ofh, starting with the pixel at columnxand rowy.
-
copy()¶ Return an exact copy of the image.
-
invert()¶ Return a new image by inverting the brightness of the pixels in the source image.
-
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.
-
blit(src, x, y, w, h, xdest=0, ydest=0)¶ Copy the rectangle defined by
x,y,w,hfrom the imagesrcinto this image atxdest,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()andcrop()can are all implemented by usingblit(). 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.HEARTImage.HEART_SMALLImage.HAPPYImage.SMILEImage.SADImage.CONFUSEDImage.ANGRYImage.ASLEEPImage.SURPRISEDImage.SILLYImage.FABULOUSImage.MEHImage.YESImage.NOImage.CLOCK12,Image.CLOCK11,Image.CLOCK10,Image.CLOCK9,Image.CLOCK8,Image.CLOCK7,Image.CLOCK6,Image.CLOCK5,Image.CLOCK4,Image.CLOCK3,Image.CLOCK2,Image.CLOCK1Image.ARROW_N,Image.ARROW_NE,Image.ARROW_E,Image.ARROW_SE,Image.ARROW_S,Image.ARROW_SW,Image.ARROW_W,Image.ARROW_NWImage.TRIANGLEImage.TRIANGLE_LEFTImage.CHESSBOARDImage.DIAMONDImage.DIAMOND_SMALLImage.SQUAREImage.SQUARE_SMALLImage.RABBITImage.COWImage.MUSIC_CROTCHETImage.MUSIC_QUAVERImage.MUSIC_QUAVERSImage.PITCHFORKImage.XMASImage.PACMANImage.TARGETImage.TSHIRTImage.ROLLERSKATEImage.DUCKImage.HOUSEImage.TORTOISEImage.BUTTERFLYImage.STICKFIGUREImage.GHOSTImage.SWORDImage.GIRAFFEImage.SKULLImage.UMBRELLAImage.SNAKE
Finally, related collections of images have been grouped together:
* ``Image.ALL_CLOCKS``
* ``Image.ALL_ARROWS``
Operations¶
repr(image)
Get a compact string representation of the image.
str(image)
Get a readable string representation of the image.
image1 + image2
Create a new image by adding the brightness values from the two images for each pixel.
image * n
Create a new image by multiplying the brightness of each pixel by n.