[v7][ESP32] Convert a JPEG/PNG image to .c file and save it

What MCU/Processor/Board and compiler are you using?

ESP32, ESP-IDF

What LVGL version are you using?

7.3.1

What do you want to achieve?

I’m downloading images (jpeg/png) from a server and saving them on my ESP32 on SPIFFS. I want to display these images in my display using LVGL.

My idea would be to convert the image (jpeg/png) saved in memory (spiffs) to a LVGL compatible .c file. Then save that file to memory (spiffs).

What have you tried so far?

I haven’t tried anything yet, I’ve only read some documentation.

From what I found it’s possible to convert a png to a lv_img_dsct_t variable, but this would mean I have to convert the images every time I want to use them.

This is not very viable since the images will be creating a sort of screen-saver, so I need to display them in succession and several times (they also occupy the whole screen).

My ideas

Right now I believe there are two viable options.

  1. Take the variables returned by the png descriptor and create a routine to create a file and save it to SPIFFS, then delete the jpg/png file. (very time consuming on programming and resource consuming on-device).
  2. Download a .c file directly from the server - This would be ideal, but I have no way of automatically convert images to LVGL’s .c file like the Online Converter does and converting them manually isn’t an option. Unless there is an API for the online converter that could be used by a back-end to automate this process? @kisvegabor I guess only you could answer this question.

this maybe give an idea

1 Like

If you are looking for a consistent API you could give https://github.com/embeddedt/lv_img_conv a try; it is a work in progress and I have not added support for the binary format yet but it can produce C files. Let me know if you have any questions about how the command line options work as there is not really any documentation yet.

This is planned to replace the PHP converter on our website eventually because the PHP version has limitations with regards to file type and image size.

2 Likes

If you can convert them to bitmap with always the same size, you can them use them as lv_img if you manipulate the “map” field.

1 Like

Thank you all for your answers, it really helped in solving this issue.

Now I’ve run into another wall. How can I display my image?
As in: I have a .c file, but how do I know how to read it? How do I get the bit_map name out of it?

My file is stored in SPIFFS as img1.c. In a normal image stored in my firmware I would just use LV_IMG_DECLARE(img1) and then set it as source for some image object, etc.
But this way, it being downloaded and in a dynamic memory partition with a dynamic name, how should I go about this?
I was thinking about saving the pointer to the images I download and then pass those pointers when I need the images. But this doesn’t sound like a very good method.

You can’t display a C image directly, because it’s a C file, not a binary. You would want to convert it to the binary format, then you can use LVGL’s filesystem interface to display it.

Thanks for the tip!
Sounds like I’ll have to hardcode the whole process.