jpg

Note

This module is only applicable to the CoreS3 Controller

jpg module for encoding and decoding operations of JPG format images

Micropython Example

take photo

Click the screen to start the countdown and take a photo

 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
 9import time
10import jpg
11
12
13file_0 = None
14img = None
15cnt = None
16last_time = None
17index = None
18img_jpg = None
19
20
21def setup():
22    global file_0, img, cnt, last_time, index, img_jpg
23    M5.begin()
24    Widgets.fillScreen(0x222222)
25    camera.init(pixformat=camera.RGB565, framesize=camera.QVGA)
26    cnt = 0
27    last_time = 0
28    index = 0
29
30
31def loop():
32    global file_0, img, cnt, last_time, index, img_jpg
33    M5.update()
34    img = camera.snapshot()
35    if (M5.Touch.getCount()) > 0:
36        cnt = 5
37        print("hello M5")
38    if cnt > 0:
39        if (time.ticks_diff((time.ticks_ms()), last_time)) >= 1000:
40            last_time = time.ticks_ms()
41            cnt = cnt - 1
42            if cnt == 0:
43                img_jpg = jpg.encode(img, 80)
44                index = index + 1
45                file_0 = open(
46                    "/flash/" + str((str("photo") + str((str(index) + str(".jpg"))))), "w"
47                )
48                file_0.write(img_jpg.bytearray())
49                file_0.close()
50        img.draw_string(140, 80, str(cnt), color=0x000099, scale=5)
51    M5.Lcd.show(img, 0, 0, 320, 240)
52
53
54if __name__ == "__main__":
55    try:
56        setup()
57        while True:
58            loop()
59    except (Exception, KeyboardInterrupt) as e:
60        try:
61            from utility import print_error_msg
62
63            print_error_msg(e)
64        except ImportError:
65            print("please update to latest firmware")

UIFlow2.0 Example

take photo

Click the screen to start the countdown and take a photo

take_photo_example.png

take_photo_example.m5f2

Methods

jpg.encode(img: image.Image, quality=60) image.Image

encode to jog photo

  • img Image to be encoded, in the format of image.RGB565

Return image.Image instance, image format image.JPEG

UIFlow2.0

encode.png

jpg.decode(img_jpg: image.Image) image.Image

jpg photo decode

  • img Image to be decoded, in the format of image.JPEG

Return image.Image instance, image format image.RGB565

UIFlow2.0

decode.png