image

备注

当前模块只适用于 CoreS3 主机

Micropython 案例

绘图测试

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4import os, sys, io
 5import M5
 6from M5 import *
 7import camera
 8import image
 9
10img = None
11
12def setup():
13    global img
14    M5.begin()
15    Widgets.fillScreen(0x222222)
16    camera.init(pixformat=camera.RGB565, framesize=camera.QVGA)
17
18def loop():
19    global img
20    M5.update()
21    img = camera.snapshot()
22    img.draw_string(10, 10, str('M5Stack'), color=0x3366ff, scale=2)
23    img.draw_rectangle(60, 80, 50, 40, color=0x33cc00, thickness=3, fill=False)
24    img.draw_line(200, 60, 260, 100, color=0xff0000, thickness=3)
25    img.draw_circle(160, 120, 30, color=0xffcc00, thickness=2, fill=False)
26    M5.Lcd.show(img, 0, 0, 320, 240)
27
28if __name__ == '__main__':
29    try:
30        setup()
31        while True:
32            loop()
33    except (Exception, KeyboardInterrupt) as e:
34        try:
35            from utility import print_error_msg
36            print_error_msg(e)
37        except ImportError:
38            print("please update to latest firmware")

UIFlow2.0 Example

绘图测试

image_draw_example.png

cores3_example_draw_test.m5f2

class image.Image

image.Image 对象由 camera.snapshot() 返回.

Basic Methods

image.width() int

返回图像的宽度(以像素为单位)。

UIFlow2.0

width.png

image.height() int

返回图像的高度(以像素为单位)。

UIFlow2.0

height.png

image.format() int

返回图像的格式

UIFlow2.0

format.png

image.size() int

返回图像的大小(以字节为单位)。

UIFlow2.0

size.png

image.bytearray() bytearray

返回一个 bytearray 对象,该对象指向图像数据,允许进行字节级别的读写访问。

UIFlow2.0

bytearray.png

Drawing Methods

image.draw_line(x0: int, y0: int, x1: int, y1: int, color: int | Tuple[int, int, int] | None = None, thickness=1) Image

在图像上绘制一条从 (x0, y0) 到 (x1, y1) 的线段。你可以分别传递 x0, y0, x1, y1,或者将它们作为元组 (x0, y0, x1, y1) 一起传递。

  • color 颜色值 RGB888 格式,可以是 (r, g, b) 元组或整数。

  • thickness 控制线条的粗细(以像素为单位)。

返回 image.Image 对象,以便你可以使用 . 表示法调用其他方法。

UIFlow2.0

draw_line.png

image.draw_rectangle(x: int, y: int, w: int, h: int, color: int | Tuple[int, int, int] | None = None, thickness=1, fill=False) Image

在图像上绘制一个矩形。你可以分别传递 x, y, w, h,或者将它们作为元组 (x, y, w, h) 一起传递。

  • color 颜色值 RGB888 格式,可以是 (r, g, b) 元组或整数。

  • thickness 控制线条的粗细(以像素为单位)。

  • fill 是否填充。

返回 image.Image 对象,以便你可以使用 . 表示法调用其他方法。

UIFlow2.0

draw_rectangle.png

image.draw_circle(x: int, y: int, radius: int, color: int | Tuple[int, int, int] | None = None, thickness=1, fill=False) Image

在图像上绘制一个圆形。你可以分别传递 x, y, radius,或者将它们作为元组 (x, y, radius) 一起传递。

  • color 颜色值 RGB888 格式,可以是 (r, g, b) 元组或整数。

  • thickness 厚度”控制边缘的像素厚度。

  • fill set to True to fill the circle.

返回 image.Image 对象,以便你可以使用 . 表示法调用其他方法。

UIFlow2.0

draw_circle.png

image.draw_string(x: int, y: int, text: str, color: int | Tuple[int, int, int] | None = None, scale=1) Image

在图像上绘制从位置 (x, y) 开始的 8x16 文本。你可以分别传递 x, y,或者将它们作为元组 (x, y) 一起传递。

  • text 是要写入图像的字符串。`` `` 和 `` `` 换行符将光标移动到下一行。

  • color 颜色值 RGB888 格式,可以是 (r, g, b) 元组或整数。

  • scale 可增加或减少图像中文本的大小。你可以传递大于 0 的整数或浮动值。

返回 image.Image 对象,以便你可以使用 . 表示法调用其他方法。

UIFlow2.0

draw_string.png

Constants

image.RGB565: int

RGB565 像素格式。每个像素为 16 位,2 字节。5 位用于红色,6 位用于绿色,5 位用于蓝色。

image.GRAYSCALE: int

灰度图像像素格式。每个像素为 8 位,1 字节。

image.JPEG: int

A JPEG image.

image.YUV422: int

一种非常适合 JPEG 压缩的像素格式。每个像素存储为一个灰度 8 位 Y 值,后跟交替的 8 位 U/V 色彩值,这些值在两个 Y 值之间共享(8 位 Y1,8 位 U,8 位 Y2,8 位 V,依此类推)。只有一些图像处理方法可以与 YUV422 格式兼容。

UIFlow2.0

format_option.png