Image (lv_img)
Overview
Images are the basic object to display images from flash (as arrays) or
from files. Images can display symbols (LV_SYMBOL_...
) too.
Using the Image decoder interface custom image formats can be supported as well.
Parts and Styles
LV_PART_MAIN
A background rectangle that uses the typical background style properties and the image itself using the image style properties.
Usage
Image source
To provide maximum flexibility, the source of the image can be:
a variable in code (a C array with the pixels).
a file stored externally (e.g. on an SD card).
a text with Symbols.
To set the source of an image, use lv_image_set_src(img, src).
To generate a pixel array from a PNG, JPG or BMP image, use the Online image converter tool and set the converted image with its pointer lv_image_set_src(img1, &converted_img_var) To make the variable visible in the C file, you need to declare it with LV_IMG_DECLARE(converted_img_var).
To use external files, you also need to convert the image files using the online converter tool but now you should select the binary output format. You also need to use LVGL's file system module and register a driver with some functions for the basic file operation. Go to the File system to learn more. To set an image sourced from a file, use lv_image_set_src(img, "S:folder1/my_img.bin").
You can also set a symbol similarly to Labels. In this case, the image will be rendered as text according to the font specified in the style. It enables to use of light-weight monochrome "letters" instead of real images. You can set symbol like lv_image_set_src(img1, LV_SYMBOL_OK).
Label as an image
Images and labels are sometimes used to convey the same thing. For
example, to describe what a button does. Therefore, images and labels
are somewhat interchangeable, that is the images can display texts by
using LV_SYMBOL_DUMMY
as the prefix of the text. For example,
lv_image_set_src(img, LV_SYMBOL_DUMMY, "Some text").
Transparency
The internal (variable) and external images support 2 transparency handling methods:
Alpha byte - An alpha byte is added to every pixel that contains the pixel's opacity
Palette and Alpha index
Besides the True color (RGB) color format, the following formats are supported:
Indexed: Image has a palette.
Alpha indexed: Only alpha values are stored.
These options can be selected in the image converter. To learn more about the color formats, read the Images section.
Recolor
A color can be mixed with every pixel of an image with a given
intensity. This can be useful to show different states (checked,
inactive, pressed, etc.) of an image without storing more versions of
the same image. This feature can be enabled in the style by setting
img_recolor_opa
between LV_OPA_TRANSP
(no recolor, value: 0) and
LV_OPA_COVER
(full recolor, value: 255). The default value is
LV_OPA_TRANSP
so this feature is disabled.
The color to mix is set by img_recolor
.
Auto-size
If the width or height of the image object is set to LV_SIZE_CONTENT
the object's size will be set according to the size of the image source
in the respective direction.
Mosaic
If the object's size is greater than the image size in any directions, then the image will be repeated like a mosaic. This allows creation a large image from only a very narrow source. For example, you can have a 300 x 5 image with a special gradient and set it as a wallpaper using the mosaic feature.
Offset
With lv_image_set_offset_x(img, x_ofs) and lv_image_set_offset_y(img, y_ofs), you can add some offset to the displayed image. Useful if the object size is smaller than the image source size. Using the offset parameter a Texture atlas or a "running image" effect can be created by Animating the x or y offset.
Transformations
Using the lv_image_set_scale(img, factor) the images will be zoomed.
Set factor
to 256
or LV_ZOOM_NONE
to disable zooming. A
larger value enlarges the images (e.g. 512
double size), a smaller
value shrinks it (e.g. 128
half size). Fractional scale works as
well. E.g. 281
for 10% enlargement.
To rotate the image use lv_image_set_rotation(img, angle). Angle has 0.1 degree precision, so for 45.8° set 458.
The transform_zoom
and transform_angle
style properties are also
used to determine the final zoom and angle.
By default, the pivot point of the rotation is the center of the image.
It can be changed with lv_image_set_pivot(img, pivot_x, pivot_y).
0;0
is the top left corner.
The quality of the transformation can be adjusted with lv_image_set_antialias(img, true). With enabled anti-aliasing the transformations are higher quality but slower.
The transformations require the whole image to be available. Therefore
indexed images (LV_IMG_CF_INDEXED_...
), alpha only images
(LV_IMG_CF_ALPHA_...
) or images from files can not be transformed.
In other words transformations work only on true color images stored as
C array, or if a custom Image decoder
returns the whole image.
Note that the real coordinates of image objects won't change during
transformation. That is lv_obj_get_width/height/x/y()
will return
the original, non-zoomed coordinates.
IMPORTANT The transformation of the image is independent of the transformation properties coming from styles. (See here). The main differences are that pure image widget transformation
doesn't transform the children of the image widget
image is transformed directly without creating an intermediate layer (buffer) to snapshot the widget
Size mode
By default, when the image is zoomed or rotated the real coordinates of the image object are not changed. The larger content simply overflows the object's boundaries. It also means the layouts are not affected the by the transformations.
If you need the object size to be updated to the transformed size set
lv_image_set_size_mode(img, LV_IMAGE_SIZE_MODE_REAL). (The previous mode
is the default and called LV_IMAGE_SIZE_MODE_VIRTUAL
). In this case if
the width/height of the object is set to LV_SIZE_CONTENT
the
object's size will be set to the zoomed and rotated size. If an explicit
size is set then the overflowing content will be cropped.
Rounded image
You can use lv_obj_set_style_radius()
to set radius to an image, and
enable lv_obj_set_style_clip_corner()
to clip the content to rounded
rectangle or circular shape. Please note this will have some negative
performance impact to CPU based renderers.
Events
No special events are sent by image objects.
See the events of the Base object too.
Learn more about Events.
Keys
No Keys are processed by the object type.
Learn more about Keys.
Example
Image from variable and symbol
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_IMG && LV_BUILD_EXAMPLES
void lv_example_image_1(void)
{
LV_IMAGE_DECLARE(img_cogwheel_argb);
lv_obj_t * img1 = lv_image_create(lv_screen_active());
lv_image_set_src(img1, &img_cogwheel_argb);
lv_obj_align(img1, LV_ALIGN_CENTER, 0, -20);
lv_obj_set_size(img1, 200, 200);
lv_obj_t * img2 = lv_image_create(lv_screen_active());
lv_image_set_src(img2, LV_SYMBOL_OK "Accept");
lv_obj_align_to(img2, img1, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
}
#endif
#!/opt/bin/lv_micropython -i
import usys as sys
import lvgl as lv
import display_driver
# Create an image from the png file
try:
with open('../../assets/image_cogwheel_argb.png','rb') as f:
png_data = f.read()
except:
print("Could not find image_cogwheel_argb.png")
sys.exit()
image_cogwheel_argb = lv.image_dsc_t({
'data_size': len(png_data),
'data': png_data
})
image1 = lv.image(lv.screen_active())
image1.set_src(image_cogwheel_argb)
image1.align(lv.ALIGN.CENTER, 0, -20)
image1.set_size(200, 200)
image2 = lv.image(lv.screen_active())
image2.set_src(lv.SYMBOL.OK + "Accept")
image2.align_to(image1, lv.ALIGN.OUT_BOTTOM_MID, 0, 20)
Image recoloring
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_IMG && LV_USE_SLIDER && LV_BUILD_EXAMPLES
static lv_obj_t * create_slider(lv_color_t color);
static void slider_event_cb(lv_event_t * e);
static lv_obj_t * red_slider, * green_slider, * blue_slider, * intense_slider;
static lv_obj_t * img1;
/**
* Demonstrate runtime image re-coloring
*/
void lv_example_image_2(void)
{
/*Create 4 sliders to adjust RGB color and re-color intensity*/
red_slider = create_slider(lv_palette_main(LV_PALETTE_RED));
green_slider = create_slider(lv_palette_main(LV_PALETTE_GREEN));
blue_slider = create_slider(lv_palette_main(LV_PALETTE_BLUE));
intense_slider = create_slider(lv_palette_main(LV_PALETTE_GREY));
lv_slider_set_value(red_slider, LV_OPA_20, LV_ANIM_OFF);
lv_slider_set_value(green_slider, LV_OPA_90, LV_ANIM_OFF);
lv_slider_set_value(blue_slider, LV_OPA_60, LV_ANIM_OFF);
lv_slider_set_value(intense_slider, LV_OPA_50, LV_ANIM_OFF);
lv_obj_align(red_slider, LV_ALIGN_LEFT_MID, 25, 0);
lv_obj_align_to(green_slider, red_slider, LV_ALIGN_OUT_RIGHT_MID, 25, 0);
lv_obj_align_to(blue_slider, green_slider, LV_ALIGN_OUT_RIGHT_MID, 25, 0);
lv_obj_align_to(intense_slider, blue_slider, LV_ALIGN_OUT_RIGHT_MID, 25, 0);
/*Now create the actual image*/
LV_IMAGE_DECLARE(img_cogwheel_argb)
img1 = lv_image_create(lv_screen_active());
lv_image_set_src(img1, &img_cogwheel_argb);
lv_obj_align(img1, LV_ALIGN_RIGHT_MID, -20, 0);
lv_obj_send_event(intense_slider, LV_EVENT_VALUE_CHANGED, NULL);
}
static void slider_event_cb(lv_event_t * e)
{
LV_UNUSED(e);
/*Recolor the image based on the sliders' values*/
lv_color_t color = lv_color_make(lv_slider_get_value(red_slider), lv_slider_get_value(green_slider),
lv_slider_get_value(blue_slider));
lv_opa_t intense = lv_slider_get_value(intense_slider);
lv_obj_set_style_image_recolor_opa(img1, intense, 0);
lv_obj_set_style_image_recolor(img1, color, 0);
}
static lv_obj_t * create_slider(lv_color_t color)
{
lv_obj_t * slider = lv_slider_create(lv_screen_active());
lv_slider_set_range(slider, 0, 255);
lv_obj_set_size(slider, 10, 200);
lv_obj_set_style_bg_color(slider, color, LV_PART_KNOB);
lv_obj_set_style_bg_color(slider, lv_color_darken(color, LV_OPA_40), LV_PART_INDICATOR);
lv_obj_add_event(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
return slider;
}
#endif
#!/opt/bin/lv_micropython -i
import usys as sys
import lvgl as lv
import display_driver
# Create an image from the png file
try:
with open('../../assets/image_cogwheel_argb.png','rb') as f:
png_data = f.read()
except:
print("Could not find image_cogwheel_argb.png")
sys.exit()
image_cogwheel_argb = lv.image_dsc_t({
'data_size': len(png_data),
'data': png_data
})
def create_slider(color):
slider = lv.slider(lv.screen_active())
slider.set_range(0, 255)
slider.set_size(10, 200)
slider.set_style_bg_color(color, lv.PART.KNOB)
slider.set_style_bg_color(color.darken(lv.OPA._40), lv.PART.INDICATOR)
slider.add_event(slider_event_cb, lv.EVENT.VALUE_CHANGED, None)
return slider
def slider_event_cb(e):
# Recolor the image based on the sliders' values
color = lv.color_make(red_slider.get_value(), green_slider.get_value(), blue_slider.get_value())
intense = intense_slider.get_value()
image1.set_style_image_recolor_opa(intense, 0)
image1.set_style_image_recolor(color, 0)
#
# Demonstrate runtime image re-coloring
#
# Create 4 sliders to adjust RGB color and re-color intensity
red_slider = create_slider(lv.palette_main(lv.PALETTE.RED))
green_slider = create_slider(lv.palette_main(lv.PALETTE.GREEN))
blue_slider = create_slider(lv.palette_main(lv.PALETTE.BLUE))
intense_slider = create_slider(lv.palette_main(lv.PALETTE.GREY))
red_slider.set_value(lv.OPA._20, lv.ANIM.OFF)
green_slider.set_value(lv.OPA._90, lv.ANIM.OFF)
blue_slider.set_value(lv.OPA._60, lv.ANIM.OFF)
intense_slider.set_value(lv.OPA._50, lv.ANIM.OFF)
red_slider.align(lv.ALIGN.LEFT_MID, 25, 0)
green_slider.align_to(red_slider, lv.ALIGN.OUT_RIGHT_MID, 25, 0)
blue_slider.align_to(green_slider, lv.ALIGN.OUT_RIGHT_MID, 25, 0)
intense_slider.align_to(blue_slider, lv.ALIGN.OUT_RIGHT_MID, 25, 0)
# Now create the actual image
image1 = lv.image(lv.screen_active())
image1.set_src(image_cogwheel_argb)
image1.align(lv.ALIGN.RIGHT_MID, -20, 0)
intense_slider.send_event(lv.EVENT.VALUE_CHANGED, None)
Rotate and zoom
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_IMG && LV_BUILD_EXAMPLES
static void set_angle(void * img, int32_t v)
{
lv_image_set_rotation(img, v);
}
static void set_zoom(void * img, int32_t v)
{
lv_image_set_zoom(img, v);
}
/**
* Show transformations (zoom and rotation) using a pivot point.
*/
void lv_example_image_3(void)
{
LV_IMAGE_DECLARE(img_cogwheel_argb);
/*Now create the actual image*/
lv_obj_t * img = lv_image_create(lv_screen_active());
lv_image_set_src(img, &img_cogwheel_argb);
lv_obj_align(img, LV_ALIGN_CENTER, 50, 50);
lv_image_set_pivot(img, 0, 0); /*Rotate around the top left corner*/
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, img);
lv_anim_set_exec_cb(&a, set_angle);
lv_anim_set_values(&a, 0, 3600);
lv_anim_set_time(&a, 5000);
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
lv_anim_start(&a);
lv_anim_set_exec_cb(&a, set_zoom);
lv_anim_set_values(&a, 128, 256);
lv_anim_set_playback_time(&a, 3000);
lv_anim_start(&a);
}
#endif
#!/opt/bin/lv_micropython -i
import usys as sys
import lvgl as lv
import display_driver
# Create an image from the png file
try:
with open('../../assets/image_cogwheel_argb.png','rb') as f:
png_data = f.read()
except:
print("Could not find image_cogwheel_argb.png")
sys.exit()
image_cogwheel_argb = lv.image_dsc_t({
'data_size': len(png_data),
'data': png_data
})
def set_angle(image, v):
image.set_angle(v)
def set_zoom(image, v):
image.set_zoom(v)
#
# Show transformations (zoom and rotation) using a pivot point.
#
# Now create the actual image
image = lv.image(lv.screen_active())
image.set_src(image_cogwheel_argb)
image.align(lv.ALIGN.CENTER, 50, 50)
image.set_pivot(0, 0) # Rotate around the top left corner
a1 = lv.anim_t()
a1.init()
a1.set_var(image)
a1.set_custom_exec_cb(lambda a,val: set_angle(image,val))
a1.set_values(0, 3600)
a1.set_time(5000)
a1.set_repeat_count(lv.ANIM_REPEAT_INFINITE)
lv.anim_t.start(a1)
a2 = lv.anim_t()
a2.init()
a2.set_var(image)
a2.set_custom_exec_cb(lambda a,val: set_zoom(image,val))
a2.set_values(128, 256)
a2.set_time(5000)
a2.set_playback_time(3000)
a2.set_repeat_count(lv.ANIM_REPEAT_INFINITE)
lv.anim_t.start(a2)
Image offset and styling
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_IMG && LV_BUILD_EXAMPLES
static void ofs_y_anim(void * img, int32_t v)
{
lv_image_set_offset_y(img, v);
}
/**
* Image styling and offset
*/
void lv_example_image_4(void)
{
LV_IMAGE_DECLARE(img_skew_strip);
static lv_style_t style;
lv_style_init(&style);
lv_style_set_bg_color(&style, lv_palette_main(LV_PALETTE_YELLOW));
lv_style_set_bg_opa(&style, LV_OPA_COVER);
lv_style_set_image_recolor_opa(&style, LV_OPA_COVER);
lv_style_set_image_recolor(&style, lv_color_black());
lv_obj_t * img = lv_image_create(lv_screen_active());
lv_obj_add_style(img, &style, 0);
lv_image_set_src(img, &img_skew_strip);
lv_obj_set_size(img, 150, 100);
lv_obj_center(img);
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, img);
lv_anim_set_exec_cb(&a, ofs_y_anim);
lv_anim_set_values(&a, 0, 100);
lv_anim_set_time(&a, 3000);
lv_anim_set_playback_time(&a, 500);
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
lv_anim_start(&a);
}
#endif
def ofs_y_anim(image, v):
image.set_offset_y(v)
# print(image,v)
# Create an image from the png file
try:
with open('../../assets/img_skew_strip.png','rb') as f:
png_data = f.read()
except:
print("Could not find img_skew_strip.png")
sys.exit()
image_skew_strip = lv.image_dsc_t({
'data_size': len(png_data),
'data': png_data
})
#
# Image styling and offset
#
style = lv.style_t()
style.init()
style.set_bg_color(lv.palette_main(lv.PALETTE.YELLOW))
style.set_bg_opa(lv.OPA.COVER)
style.set_image_recolor_opa(lv.OPA.COVER)
style.set_image_recolor(lv.color_black())
image = lv.image(lv.screen_active())
image.add_style(style, 0)
image.set_src(image_skew_strip)
image.set_size(150, 100)
image.center()
a = lv.anim_t()
a.init()
a.set_var(image)
a.set_values(0, 100)
a.set_time(3000)
a.set_playback_time(500)
a.set_repeat_count(lv.ANIM_REPEAT_INFINITE)
a.set_custom_exec_cb(lambda a,val: ofs_y_anim(image,val))
lv.anim_t.start(a)