""" Iamge MicroPython library for the Iamge(jpeg C module) ======================================================= @dahanzimin From the Mixly Team """ import gc import urequests from base64 import b64encode from jpeg import Encoder, Decoder class IMG: def __init__(self, image, width, height): self.image = image self.width = width self.height = height self.format = "RGB565" class Image: def save(self, img, path="mixly.jpg", **kwargs): '''quality(1-100), rotation (0, 90, 180, 270)''' _encoder = Encoder(pixel_format="RGB565_BE", width=img.width, height=img.height, **kwargs) _jpeg = _encoder.encode(img.image) del _encoder gc.collect() if isinstance(path, str): with open(path, 'wb') as f: f.write(_jpeg) else: return _jpeg def open(self, path="mixly.jpg", scale_width=None, scale_height=None, tft_width=240, tft_height=240, **kwargs): '''rotation (0, 90, 180, 270), clipper_width, clipper_height''' with open(path, "rb") as f: _jpeg = f.read() return self._jpg_decoder(_jpeg, scale_width, scale_height, tft_width, tft_height, **kwargs) def convert(self, img, formats=0, **kwargs): if formats == 0: return self.save(img, None, **kwargs) elif formats == 1: return b'data:image/jpg;base64,' + b64encode(self.save(img, None, **kwargs)) def download(self, url, path=None, scale_width=None, scale_height=None, tft_width=240, tft_height=240, block=1024, **kwargs): '''rotation (0, 90, 180, 270), clipper_width, clipper_height''' response = urequests.get(url, stream=True) if path is None: _image = self._jpg_decoder(response.raw.read(), scale_width, scale_height, tft_width, tft_height, **kwargs) response.close() return _image else: with open(path, 'wb') as f: while True: _data = response.raw.read(block) if not _data: break else: f.write(_data) response.close() def _jpg_decoder(self, jpg, scale_width, scale_height, tft_width, tft_height, **kwargs): '''Automatically zoom based on the screen''' if scale_width is None or scale_height is None: _width = tft_width _height = tft_height for i in range(min(len(jpg), 1024)): if jpg[i] == 0xFF and (jpg[i + 1] & 0xF0) == 0xC0: if jpg[i + 1] not in [0xC4, 0xC8, 0xCC]: _width = jpg[i + 7] << 8 | jpg[i + 8] _height = jpg[i + 5] << 8| jpg[i + 6] break if _width > tft_width or _height > tft_height: _scale = max(_width / tft_width, _height / tft_height) * 8 _decoder = Decoder(pixel_format="RGB565_BE", scale_width=round(_width / _scale) * 8, scale_height=round(_height / _scale) * 8, **kwargs) else: _decoder = Decoder(pixel_format="RGB565_BE", **kwargs) else: _decoder = Decoder(pixel_format="RGB565_BE", scale_width=scale_width // 8 * 8, scale_height=scale_height // 8 * 8, **kwargs) _info = _decoder.get_img_info(jpg) _image = IMG(_decoder.decode(jpg), _info[0], _info[1]) del _decoder, jpg gc.collect() return _image #图像倄理 Image = Image()