How to convert PNG to CF_INDEXED_1_BIT for LVGL v9 for use with lv_image_set_src

What do you want to achieve?

I want to convert a png file to a LV_COLOR_FORMAT_I1 bin file for lvgl v9 as I was using CF_INDEXED_1_BIT with lvgl v8 so I can open an image file with lv_image_set_src.

If I have python locally, what is the exact all to create a test.bin from a test.png?

What have you tried so far?

I have downloaded the python file from GitHub

If you run python3.exe .\LVGLImage.py --h will get the help on how to create the command…

usage: LVGLImage.py [-h] [--ofmt {C,BIN,PNG}]
                    [--cf {L8,I1,I2,I4,I8,A1,A2,A4,A8,ARGB8888,XRGB8888,RGB565,RGB565_SWAPPED,RGB565A8,ARGB8565,RGB888,AUTO,RAW,RAW_ALPHA,ARGB8888_PREMULTIPLIED}] [--rgb565dither]
                    [--premultiply] [--compress {NONE,RLE,LZ4}] [--align [byte]] [--background [color]] [--nemagfx] [-o OUTPUT] [--name NAME] [-v]
                    input

LVGL PNG to bin image tool.

positional arguments:
  input                 the filename or folder to be recursively converted

options:
  -h, --help            show this help message and exit
  --ofmt {C,BIN,PNG}    output filename format, C or BIN
  --cf {L8,I1,I2,I4,I8,A1,A2,A4,A8,ARGB8888,XRGB8888,RGB565,RGB565_SWAPPED,RGB565A8,ARGB8565,RGB888,AUTO,RAW,RAW_ALPHA,ARGB8888_PREMULTIPLIED}
                        bin image color format, use AUTO for automatically choose from I1/2/4/8
  --rgb565dither        use dithering to correct banding in gradients
  --premultiply         pre-multiply color with alpha
  --compress {NONE,RLE,LZ4}
                        Binary data compress method, default to NONE
  --align [byte]        stride alignment in bytes for bin image
  --background [color]  Background color for formats without alpha
  --nemagfx             export color palette for I8 images in a format compatible with NEMA accelerator
  -o OUTPUT, --output OUTPUT
                        Select the output folder, default to ./output
  --name NAME           Specify name for output file. Only applies when input is a file, not a directory. (Also used for variable name inside .c file when format is 'C')
  -v, --verbose

To generate an imagem in format I1 and output in BIN file, would be something like:

python3.exe .\LVGLImage.py --ofmt BIN --cf I1 ..\..\GUIs\FDR_Simulator\src\img1_white.png

This will create an output folder where the python script is, and the output file will be placed there.

You need some extra requisites to be installed, check prerequisites, not all of them are necessary for LVGLImage-py, would say that only pypng and lz4 are probably required.

Thank you. This is very helpful. I’m using a Mac and finding it hard to install the pypnb.

This environment is externally managed
╰─> To install Python packages system-wide, try brew install
xyz, where xyz is the package you are trying to
install.

When I try: python3 -m pip install pypng

Has anybody installed the prerequisites on a Mac?

Sorry, don’t use MAC. But it seems that it may be necessary to create venv, check link and link to avoid possibility to corrupt system files (because of dependencies…).

Ok. I have a virtual envrionment and these installed:

Package Version


lz4 4.4.4
pip 25.2
pypng 0.20220715.0

But I get this error:

BaseException: cannot find pngquant tool, install it via sudo apt install pngquant for debian or brew install pngquant for macintosh For windows, you may need to download pngquant.exe from https://pngquant.org/, and put it in your PATH.

I didn’t see that in the prerequisites.

Ok, i may have that already installed in my system because of EEZ-Studio that had that requirement, and when i used this script for the first time was already included in system path.

There is a linux script to install pngquant don’t know if applicable to MACOS or if it’s easier/better to install via brew if possible.

Ok. Thanks. This worked:

brew install pngquant

I appreciate the help.