PNG decoder¶
Allow the use of PNG images in LVGL. This implementation uses lodepng library.
If enabled in lv_conf.h
by LV_USE_PNG
LVGL will register a new image decoder automatically so PNG files can be directly used as any other image sources.
Note that, a file system driver needs to registered to open images from files. Read more about it here or just enable one in lv_conf.h
with LV_USE_FS_...
The whole PNG image is decoded so during decoding RAM equals to image width x image height x 4
bytes are required.
As it might take significant time to decode PNG images LVGL's images caching feature can be useful.
Example¶
Open a PNG image from file and variable¶
C code
GitHub#include "../../lv_examples.h"
#if LV_USE_PNG && LV_USE_IMG && LV_BUILD_EXAMPLES
/**
* Open a PNG image from a file and a variable
*/
void lv_example_png_1(void)
{
LV_IMG_DECLARE(img_wink_png);
lv_obj_t * img;
img = lv_img_create(lv_scr_act());
lv_img_set_src(img, &img_wink_png);
lv_obj_align(img, LV_ALIGN_LEFT_MID, 20, 0);
img = lv_img_create(lv_scr_act());
/* Assuming a File system is attached to letter 'A'
* E.g. set LV_USE_FS_STDIO 'A' in lv_conf.h */
lv_img_set_src(img, "A:lvgl/examples/libs/png/wink.png");
lv_obj_align(img, LV_ALIGN_RIGHT_MID, -20, 0);
}
#endif
#!/opt/bin/lv_micropython -i
import lvgl as lv
import display_driver
from imagetools import get_png_info, open_png
from img_wink_png import img_wink_png_map
# Register PNG image decoder
decoder = lv.img.decoder_create()
decoder.info_cb = get_png_info
decoder.open_cb = open_png
img_wink_png = lv.img_dsc_t(
{
"header": {"always_zero": 0, "w": 50, "h": 50, "cf": lv.img.CF.RAW_ALPHA},
"data_size": 5158,
"data": img_wink_png_map,
}
)
img1 = lv.img(lv.scr_act())
img1.set_src(img_wink_png)
img1.align(lv.ALIGN.RIGHT_MID, -250, 0)
# Create an image from the png file
try:
with open('wink.png','rb') as f:
png_data = f.read()
except:
print("Could not find wink.png")
sys.exit()
wink_argb = lv.img_dsc_t({
'data_size': len(png_data),
'data': png_data
})
img2 = lv.img(lv.scr_act())
img2.set_src(wink_argb)
img2.align(lv.ALIGN.RIGHT_MID, -150, 0)