{
  "id": "jacklinquan/micropython-microbmp",
  "name": "micropython-microbmp",
  "prettyName": "micropython-microbmp",
  "description": "A small Python module for BMP image processing.",
  "version": "",
  "author": "jacklinquan",
  "license": "MIT",
  "tags": [],
  "status": "VALID",
  "official": false,
  "source": "https://github.com/jacklinquan/micropython-microbmp",
  "updatedAt": "2025-06-28T07:00:19.638Z",
  "page": "https://checkmim.com/packages/jacklinquan+micropython-microbmp",
  "createdAt": "2025-05-27T04:28:50.115Z",
  "submittedBy": "oliverr",
  "install": {
    "mpremote": "mpremote mip install github:jacklinquan/micropython-microbmp",
    "mip": "import mip; mip.install(\"github:jacklinquan/micropython-microbmp\")"
  },
  "readme": "# micropython-microbmp\n[![PayPal Donate][paypal_img]][paypal_link]\n[![PyPI version][pypi_img]][pypi_link]\n[![Downloads][downloads_img]][downloads_link]\n\n  [paypal_img]: https://github.com/jacklinquan/images/blob/master/paypal_donate_badge.svg\n  [paypal_link]: https://www.paypal.me/jacklinquan\n  [pypi_img]: https://badge.fury.io/py/micropython-microbmp.svg\n  [pypi_link]: https://badge.fury.io/py/micropython-microbmp\n  [downloads_img]: https://pepy.tech/badge/micropython-microbmp\n  [downloads_link]: https://pepy.tech/project/micropython-microbmp\n\n[Try `micropython-microbmp` in browser.](https://jacklinquan.github.io/micropython-microbmp/)\n\nA small Python module for BMP image processing.\n \nIt supports BMP image of 1/2/4/8/24-bit colour depth.\n\nLoading supports compression method:\n\n- 0(BI_RGB, no compression)\n- 1(BI_RLE8, RLE 8-bit/pixel)\n- 2(BI_RLE4, RLE 4-bit/pixel)\n\nSaving only supports compression method 0(BI_RGB, no compression).\n\nThe [API][api_link] is compatible with the CPython version [microbmp][microbmp_link].\nAs a pure python module, it's not fast. But it opens up the possibility to save images.\nIt is especially useful for small IR cameras/sensors.\n\n  [api_link]: https://microbmp.readthedocs.io/en/latest/?badge=latest\n  [microbmp_link]: https://github.com/jacklinquan/microbmp\n\n## Where this module can be useful\nThis module can be useful in many scenarios, not limited to the list below:\n\n- To show BMP images on the screen.\n    It supports 1/2/4/8/24-bit colour depth and RLE compression(4-bit and 8-bit).\n    If the number of colours used in an image is small, it can be much compact.\n- To print the screen.\n    A screen or any `framebuf.FrameBuffer` object can be saved as a BMP image.\n- To save camera or IR thermal camera images.\n    For projects that involve MLX90640 or AMG88xx, the IR images can be saved.\n- To generate dynamic BMP images for web servers.\n    This module also can write BMP images to BytesIO.\n    So it does NOT have to save the images in the file system.\n    A combination of web server and IR camera can show IR image dynamically in the browser.\n\n## Installation\nThe Python file `microbmp.py` can be installed on target hardware with `mpremote`:\n```shell\npip install mpremote\nmpremote mip install github:jacklinquan/micropython-microbmp\n```\nAlternatively just copy microbmp.py to the MicroPython device.\n\n## Usage\n```Python\n>>> from microbmp import MicroBMP\n>>> img_24b_2x2 = MicroBMP(2, 2, 24)  # Create a 2(width) by 2(height) 24-bit image.\n>>> img_24b_2x2.palette  # 24-bit image has no palette.\n>>> img_24b_2x2.parray  # Pixels are arranged horizontally (top-down) in RGB order.\nbytearray(b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00')\n>>> img_24b_2x2[1, 1] = 255, 255, 255  # Access 1 pixel (R, G, B): img[x, y]\n>>> img_24b_2x2[0, 1, 0] = 255  # Access 1 primary colour of 1 pixel (Red): img[x, y, c]\n>>> img_24b_2x2[1, 0, 1] = 255  # (Green)\n>>> img_24b_2x2[0, 0, 2] = 255  # (Blue)\n>>> img_24b_2x2.save(\"img_24b_2x2.bmp\")\n70\n>>> new_img_24b_2x2 = MicroBMP().load(\"img_24b_2x2.bmp\")\n>>> new_img_24b_2x2.palette\n>>> new_img_24b_2x2.parray\nbytearray(b'\\x00\\x00\\xff\\x00\\xff\\x00\\xff\\x00\\x00\\xff\\xff\\xff')\n>>> print(new_img_24b_2x2)\nBMP image, RGB, 24-bit, 2x2 pixels, 70 bytes\n>>> img_1b_3x2 = MicroBMP(3, 2, 1)  # Create a 3(width) by 2(height) 1-bit image.\n>>> img_1b_3x2.palette  # Each colour is in the order of (R, G, B)\n[bytearray(b'\\x00\\x00\\x00'), bytearray(b'\\xff\\xff\\xff')]\n>>> img_1b_3x2.parray  # Each bit stores the colour index in HLSB format.\nbytearray(b'\\x00')\n>>> \" \".join([\"{:0>8}\".format(bin(byte)[2:]) for byte in img_1b_3x2.parray])\n'00000000'\n>>> img_1b_3x2[1, 0] = 1  # Access 1 pixel (index): img[x, y]\n>>> img_1b_3x2[1, 1] = 1\n>>> img_1b_3x2[2, 1] = 1\n>>> img_1b_3x2.save(\"img_1b_3x2.bmp\")\n70\n>>> new_img_1b_3x2 = MicroBMP().load(\"img_1b_3x2.bmp\")\n>>> new_img_1b_3x2.palette\n[bytearray(b'\\x00\\x00\\x00'), bytearray(b'\\xff\\xff\\xff')]\n>>> new_img_1b_3x2.parray\nbytearray(b'L')\n>>> \" \".join([\"{:0>8}\".format(bin(byte)[2:]) for byte in new_img_1b_3x2.parray])\n'01001100'\n>>> print(new_img_1b_3x2)\nBMP image, indexed, 1-bit, 3x2 pixels, 70 bytes\n```\n"
}