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:
Image 类用于创建可在设备的 LED 矩阵上轻松显示的图像。给定一个图像对象,可以通过显示 API 显示它:
display.show(Image.HAPPY)
There are four ways in which you can construct an image:
您可以通过四种方式构建映像:
Image() - Create a blank 5x5 image
Image() - 创建一个空白的 5x5 图像
Image(string) - Create an image by parsing the string, a single character returns that glyph
Image(string) - 通过解析字符串创建图像,单个字符返回该字形
Image(width, height) - Create a blank image of given size
Image(width, height) - 创建给定大小的空白图像
Image(width, height, buffer) - Create an image from the given buffer
Image(width, height, buffer) - 从给定的缓冲区创建图像
If string is used, it has to consist of digits 0-9 arranged into lines, describing the image, for example:
如果使用 string,它必须由排列成行的数字 0-9 组成,用于描述图像,例如:
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:
将创建 X 的 5×5 图像。行尾由冒号表示。也可以使用换行符 (n) 来表示行尾,如下所示:
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:
另一种表单会创建一个宽度为 columns 和 height 行。(可选 )buffer 可以是 width''×''height 整数在 0-9 范围内初始化图像:
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.
将创建全亮度的 2 x 2 像素图像。
Note 注意
Keyword arguments cannot be passed to buffer.
关键字参数不能传递给 buffer。
Return the number of columns in the image.
返回图像中的列数。
Return the numbers of rows in the image.
返回图像中的行数。
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).
将 x 列和第 y 行的像素亮度设置为 值 ,该值必须介于 0 (深色) 和 9 (亮) 之间。
This method will raise an exception when called on any of the built-in read-only images, like Image.HEART.
当对任何内置只读图像(如 Image.HEART) 调用时,此方法将引发异常。
Return the brightness of pixel at column x and row y as an integer between 0 and 9.
将 x 列和行 y 处的像素亮度以 0 到 9 之间的整数形式返回。
Return a new image created by shifting the picture left by n columns.
返回通过将图片向左移动 n 而创建的新图像 列。
Same as image.shift_left(-n).
与 image.shift_left (-n) 相同。
Return a new image created by shifting the picture up by n rows.
返回通过将图片上移 n 行而创建的新图像。
Same as image.shift_up(-n).
与 image.shift_up(-n) 相同。
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.
通过将图片裁剪为宽度 w 和高度 h(从 x 列和行 y 处的像素开始)来返回新图像。
Return an exact copy of the image.
返回图像的精确副本。
Return a new image by inverting the brightness of the pixels in the source image.
通过反转源图像中像素的亮度来返回新图像。
Set the brightness of all the pixels in the image to the value, which has to be between 0 (dark) and 9 (bright).
将图像中所有像素的亮度设置为 值 ,该值必须介于 0 (深色) 和 9 (亮) 之间。
This method will raise an exception when called on any of the built-in read-only images, like Image.HEART.
当对任何内置只读图像(如 Image.HEART) 调用时,此方法将引发异常。
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.
将 x, y, w, h 定义的矩形从图像 src 复制到位于 xdest, ydest 的此图像中。源矩形中但源图像外部的区域被视为值为 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:
shift_left()、shift_right()、shift_up()、shift_down() 和 crop() can 都是通过使用 blit() 实现的。例如,img.crop(x, y, w, h) 可以实现为:
def crop(self, x, y, w, h):
res = Image(w, h)
res.blit(self, x, y, w, h)
return res
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 类还包含自身的以下内置实例作为其属性(属性名称表示图像所代表的内容):
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.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_NW
Image.ARROW_N、Image.ARROW_NE、Image.ARROW_E、 Image.ARROW_SE、Image.ARROW_S、Image.ARROW_SW、 Image.ARROW_W、Image.ARROW_NWImage.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
Image.SCISSORS
Finally, related collections of images have been grouped together:
最后,相关的图像集合已组合在一起:
* ``Image.ALL_CLOCKS``
* ``Image.ALL_ARROWS``
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.
通过将每个像素的亮度乘以 n 来创建新图像。