Skip to content

Commit 75e64bd

Browse files
authored
Document image changes (#4506)
1 parent 5196cbb commit 75e64bd

File tree

4 files changed

+69
-25
lines changed

4 files changed

+69
-25
lines changed

components/animation.rst

+6-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ It adds additional lambda methods: ``next_frame()``, ``prev_frame()`` and ``set_
1212
- file: "animation.gif"
1313
id: my_animation
1414
resize: 100x100
15+
type: RGB565
1516
1617
The animation can be rendered just like the image component with the ``image()`` function of the display component.
1718

@@ -50,18 +51,15 @@ Configuration variables:
5051
in your display code.
5152
- **resize** (*Optional*, string): If set, this will resize all the frames to fit inside the given dimensions ``WIDTHxHEIGHT``
5253
and preserve the aspect ratio.
53-
- **type** (*Optional*): Specifies how to encode each frame internally. Defaults to ``BINARY``.
54+
- **type** (**Required**): Specifies how to encode image internally. See the :ref:`image component <display-image>` for more information.
5455

5556
- ``BINARY``: Two colors, suitable for 1 color displays or 2 color image in color displays. Uses 1 bit
56-
per pixel, 8 pixels per byte.
57-
- ``TRANSPARENT_BINARY``: One color, any pixel that is fully transparent will not be drawn, and any other pixel
58-
will be the on color. Uses 1 bit per pixel, 8 pixels per byte.
57+
per pixel, 8 pixels per byte. Only ``chroma_key`` transparency is available.
5958
- ``GRAYSCALE``: Full scale grey. Uses 8 bits per pixel, 1 pixel per byte.
60-
- ``RGB565``: Lossy RGB color stored. Uses 2 bytes per pixel.
61-
- ``RGB24``: Full RGB color stored. Uses 3 bytes per pixel.
62-
- ``RGBA``: Full RGB color stored. Uses 4 bytes per pixel. Any pixel with an alpha value < 127 will not be drawn.
59+
- ``RGB565``: Lossy RGB color stored. Uses 2 bytes per pixel, 3 with an alpha channel.
60+
- ``RGB``: Full RGB color stored. Uses 3 bytes per pixel, 4 with an alpha channel.
6361

64-
- **use_transparency** (*Optional*): If set the alpha channel of the input image will be taken into account, and pixels with alpha < 127 will not be drawn. For image types without explicit alpha channel, the color (0, 0, 1) (very dark blue) will be mapped to black, to be able to store transparency information within the image. Explicitly transparent types (``TRANSPARENT_BINARY`` and ``RGBA``) default to ``True`` and cannot be set to ``False``; other types default to ``False``.
62+
- **use_transparency** (*Optional*): If set the alpha channel of the input image will be taken into account. The possible values are ``opaque`` (default), ``chroma_key`` and ``alpha_channel``. See discussion on transparency in the :ref:`image component <display-image>`.
6563
- **loop** (*Optional*): If you want to loop over a subset of your animation (e.g. a fire animation where the fire "starts", then "burns" and "dies") you can specify some frames to loop over.
6664

6765
- **start_frame** (*Optional*, int): The frame to loop back to when ``end_frame`` is reached. Defaults to the first frame in the animation.
@@ -83,4 +81,3 @@ Actions:
8381

8482
- **id** (**Required**, :ref:`config-id`): The ID of the animation to animate.
8583
- **frame** (**Required**, int): The frame index to show next.
86-

components/image.rst

+61-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ For showing images downloaded at runtime, take a look at the :ref:`Online Image
1515
1616
image:
1717
- file: "image.png"
18+
type: binary
1819
id: my_image
1920
resize: 100x100
2021
@@ -23,12 +24,15 @@ For showing images downloaded at runtime, take a look at the :ref:`Online Image
2324
image:
2425
- file: mdi:alert-outline
2526
id: alert
27+
type: grayscale
28+
use_transparency: alpha_channel
2629
resize: 80x80
2730
2831
.. code-block:: yaml
2932
3033
image:
3134
- file: https://esphome.io/_images/logo.png
35+
type: rgb565
3236
id: esphome_logo
3337
resize: 200x162
3438
@@ -46,36 +50,70 @@ Configuration variables:
4650
in your display code.
4751
- **resize** (*Optional*, string): If set, this will resize the image to fit inside the given dimensions ``WIDTHxHEIGHT``
4852
and preserve the aspect ratio.
49-
- **type** (*Optional*): Specifies how to encode image internally. Defaults to ``BINARY`` for local and remote images and ``TRANSPARENT_BINARY`` for MDIs.
53+
- **type** (**Required**): Specifies how to encode image internally.
5054

5155
- ``BINARY``: Two colors, suitable for 1 color displays or 2 color image in color displays. Uses 1 bit
52-
per pixel, 8 pixels per byte.
53-
- ``TRANSPARENT_BINARY``: One color, any pixel that is fully transparent will not be drawn, and any other pixel
54-
will be the on color. Uses 1 bit per pixel, 8 pixels per byte.
56+
per pixel, 8 pixels per byte. Only ``chroma_key`` transparency is available.
5557
- ``GRAYSCALE``: Full scale grey. Uses 8 bits per pixel, 1 pixel per byte.
56-
- ``RGB565``: Lossy RGB color stored. Uses 2 bytes per pixel.
57-
- ``RGB24``: Full RGB color stored. Uses 3 bytes per pixel.
58-
- ``RGBA``: Full RGB color stored. Uses 4 bytes per pixel. Any pixel with an alpha value < 127 will not be drawn.
58+
- ``RGB565``: Lossy RGB color stored. Uses 2 bytes per pixel, 3 with an alpha channel.
59+
- ``RGB``: Full RGB color stored. Uses 3 bytes per pixel, 4 with an alpha channel.
5960

60-
- **use_transparency** (*Optional*): If set the alpha channel of the input image will be taken into account, and pixels with alpha < 127 will not be drawn. For image types without explicit alpha channel, the color (0, 0, 1) (very dark blue) will be mapped to black, to be able to store transparency information within the image. Explicitly transparent types (``TRANSPARENT_BINARY`` and ``RGBA``) default to ``True`` and cannot be set to ``False``; other types default to ``False``.
61+
- **use_transparency** (*Optional*): If set the alpha channel of the input image will be taken into account. The possible values are ``opaque`` (default), ``chroma_key`` and ``alpha_channel``. See discussion on transparency below.
6162

6263
- **dither** (*Optional*): Specifies which dither method used to process the image, only used in GRAYSCALE and BINARY type image. Defaults to ``NONE``. You can read more about it `here <https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=Dither#PIL.Image.Image.convert>`__ and `here <https://en.wikipedia.org/wiki/Dither>`__.
6364

64-
- ``NONE``: Every pixel convert to its nearest color.
65+
- ``NONE``: Every pixel converts to its nearest color.
6566
- ``FLOYDSTEINBERG``: Uses Floyd-Steinberg dither to approximate the original image luminosity levels.
6667

6768
.. note::
6869

6970
To use images you will need to have the python ``pillow`` package installed.
7071
Additionally, if you want to use SVG images (including MDI images), you will
71-
additionally need to have the python ``cairosvg`` package installed.
72+
additionally need to have the python ``cairosvg`` package installed. These are automatically installed when
73+
setting up ESPHome via the usual methods.
7274

73-
If you're running this as a Home Assistant add-on or with the official ESPHome docker image, it should already be installed.
75+
Grouping images by type
76+
-----------------------
7477

75-
Use ``pip install "esphome[displays]"`` to install these optional dependencies with
76-
the versions that ESPHome requires.
78+
You can group images by type to make it easier to manage them. This is useful when you have a lot of images to be encoded in the same way, and avoids having to repeat the same type for each image. The type name is used as the key for the group. For example:
7779

78-
And then later in code:
80+
.. code-block:: yaml
81+
82+
image:
83+
grayscale:
84+
- file: "image1.png"
85+
id: image1
86+
- file: "image2.png"
87+
id: image2
88+
- file: "image3.png"
89+
id: image3
90+
91+
rgb565:
92+
- file: "image4.png"
93+
id: image4
94+
- file: "image5.png"
95+
id: image5
96+
97+
In addition, the default transparency type can be set within a type group by using the transparency type as a key.
98+
99+
.. code-block:: yaml
100+
101+
image:
102+
rgb565:
103+
alpha_channel:
104+
- file: "image1.png"
105+
id: image1
106+
- file: "image2.png"
107+
id: image2
108+
opaque:
109+
- file: "image2.png"
110+
111+
Displaying Images
112+
-----------------
113+
114+
Images may be used in LVGL configurations wherever an image is required. See the :doc:`LVGL </components/lvgl/index>` documentation for more information.
115+
116+
To display an image directly on an ESPHome display, you can use the ``image`` method in the display lambda.
79117

80118
.. code-block:: yaml
81119
@@ -123,3 +161,12 @@ be supplied to modify the color used to represent the on and off bits respective
123161
124162
You can also use this to invert images in two color displays, use ``COLOR_OFF`` then ``COLOR_ON``
125163
as the additional parameters.
164+
165+
Transparency options
166+
--------------------
167+
168+
By default transparency is not used. If ``use_transparency: chroma_key`` is set then a specific colour (0, 1, 0) will be used to replace any transparent or partially transparent portions of the image. This will not be drawn when rendering the image, allowing the background to be visible.
169+
170+
If ``use_transparency: alpha_channel`` is set, then each pixel of the image will be assigned an additional byte with a transparency value. This is useful mainly when using :doc:`LVGL </components/lvgl/index>` as the ``alpha_channel`` transparency will enable smooth blending of transparent images with the background.
171+
When using the display lambda image drawing functions these will draw or not draw the pixel, no blending with the background will be done.
172+
The ``BINARY`` format only permits ``chroma_key`` transparency, which effectively turns the image into an alpha mask with one bit per pixel. GRAYSCALE images with transparency store the alpha channel only, and remain 1 byte per pixel.

components/lvgl/widgets.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ Images are the basic widgets used to display images.
804804
805805
.. note::
806806

807-
Currently ``RGB565`` type images are supported, with transparency using the optional parameter ``use_transparency`` set to ``true``. See :ref:`display-image` for how to load an image for rendering in ESPHome.
807+
Currently ``RGB565`` type images are supported, with transparency using the optional parameter ``use_transparency`` set. See :ref:`display-image` for how to load an image for rendering in ESPHome.
808808

809809
.. tip::
810810

cookbook/lvgl.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ To display a boot image with a spinner animation which disappears automatically
11901190
id: boot_logo
11911191
resize: 200x200
11921192
type: RGB565
1193-
use_transparency: true
1193+
use_transparency: alpha_channel
11941194
11951195
lvgl:
11961196
...

0 commit comments

Comments
 (0)