image

Note

This module is only applicable to the CoreS3 Controller

Micropython Example

draw test

 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
12
13def setup():
14    global img
15    M5.begin()
16    Widgets.fillScreen(0x222222)
17    camera.init(pixformat=camera.RGB565, framesize=camera.QVGA)
18
19
20def loop():
21    global img
22    M5.update()
23    img = camera.snapshot()
24    img.draw_string(10, 10, str("M5Stack"), color=0x3366FF, scale=2)
25    img.draw_rectangle(60, 80, 50, 40, color=0x33CC00, thickness=3, fill=False)
26    img.draw_line(200, 60, 260, 100, color=0xFF0000, thickness=3)
27    img.draw_circle(160, 120, 30, color=0xFFCC00, thickness=2, fill=False)
28    M5.Lcd.show(img, 0, 0, 320, 240)
29
30
31if __name__ == "__main__":
32    try:
33        setup()
34        while True:
35            loop()
36    except (Exception, KeyboardInterrupt) as e:
37        try:
38            from utility import print_error_msg
39
40            print_error_msg(e)
41        except ImportError:
42            print("please update to latest firmware")

UIFlow2.0 Example

draw test

image_draw_example.png

cores3_example_draw_test.m5f2

class image.Image

The line object is returned by camera.snapshot().

Basic Methods

image.width() int

Returns the image width in pixels.

UIFlow2.0

width.png

image.height() int

Returns the image height in pixels.

UIFlow2.0

height.png

image.format() int

Returns the image format

UIFlow2.0

format.png

image.size() int

Returns the image size in bytes.

UIFlow2.0

size.png

image.bytearray() bytearray

Returns a bytearray object that points to the image data for byte-level read/write access.

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

Draws a line from (x0, y0) to (x1, y1) on the image. You may either pass x0, y0, x1, y1 separately or as a tuple (x0, y0, x1, y1).

  • color is an RGB888 tuple for Grayscale or RGB565 images. Defaults to white. However, you may also pass the underlying pixel value (0-255) for grayscale images or a RGB565 value for RGB565 images.

  • thickness controls how thick the line is in pixels.

Returns the image object so you can call another method using . notation.

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

Draws a rectangle on the image. You may either pass x, y, w, h separately or as a tuple (x, y, w, h).

  • color is an RGB888 tuple for Grayscale or RGB565 images. Defaults to white. However, you may also pass the underlying pixel value (0-255) for grayscale images or a RGB565 value for RGB565 images.

  • thickness controls how thick the lines are in pixels.

  • fill set to True to fill the rectangle.

Returns the image object so you can call another method using . notation.

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

Draws a circle on the image. You may either pass x, y, radius separately or as a tuple (x, y, radius).

  • color is an RGB888 tuple for Grayscale or RGB565 images. Defaults to white. However, you may also pass the underlying pixel value (0-255) for grayscale images or a RGB565 value for RGB565 images.

  • thickness controls how thick the edges are in pixels.

  • fill set to True to fill the circle.

Returns the image object so you can call another method using . notation.

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

Draws 8x16 text starting at location (x, y) in the image. You may either pass x, y separately or as a tuple (x, y).

  • text is a string to write to the image. \n, \r, and \r\n line endings move the cursor to the next line.

  • color is an RGB888 tuple for Grayscale or RGB565 images. Defaults to white. However, you may also pass the underlying pixel value (0-255) for grayscale images or a RGB565 value for RGB565 images.

  • scale may be increased to increase/decrease the size of the text on the image. You can pass greater than 0 integer or floating point values.

Returns the image object so you can call another method using . notation.

UIFlow2.0

draw_string.png

Constants

image.RGB565: int

RGB565 pixel format. Each pixel is 16-bits, 2-bytes. 5-bits are used for red, 6-bits are used for green, and 5-bits are used for blue.

image.GRAYSCALE: int

GRAYSCALE pixel format. Each pixel is 8-bits, 1-byte.

image.JPEG: int

A JPEG image.

image.YUV422: int

A pixel format that is very easy to jpeg compress. Each pixel is stored as a grayscale 8-bit Y value followed by alternating 8-bit U/V color values that are shared between two Y values (8-bits Y1, 8-bits U, 8-bits Y2, 8-bits V, etc.). Only some image processing methods work with YUV422.

UIFlow2.0

format_option.png