Skip to content

Commit 07ce2a7

Browse files
Add online_image documentation (#2076)
Co-authored-by: Jesse Hills <[email protected]>
1 parent d3d125d commit 07ce2a7

File tree

5 files changed

+157
-1
lines changed

5 files changed

+157
-1
lines changed

components/display/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ See Also
610610
- :ref:`QR Code Component <display-qrcode>`
611611
- :ref:`Image Component <display-image>`
612612
- :ref:`Animation Component <display-animation>`
613+
- :ref:`Online Image <online_image>`
613614
- :ghedit:`Edit`
614615

615616
.. toctree::

components/image.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Images
99

1010
Use this component to store graphical images on the device, you can then draw the images on compatible displays.
1111

12+
For showing images downloaded at runtime, take a look at the :ref:`Online Image <online_image>` component.
13+
1214
.. code-block:: yaml
1315
1416
image:
@@ -121,4 +123,3 @@ be supplied to modify the color used to represent the on and off bits respective
121123
122124
You can also use this to invert images in two color displays, use ``COLOR_OFF`` then ``COLOR_ON``
123125
as the additional parameters.
124-

components/online_image.rst

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
.. _online_image:
2+
3+
Online Image Component
4+
======================
5+
6+
.. seo::
7+
:description: Instructions for displaying images downloaded at runtime in ESPHome.
8+
:image: image-sync-outline.svg
9+
10+
With this component you can define images that will be downloaded, decoded and drawn at runtime.
11+
12+
.. note::
13+
14+
Currently only images in PNG format are supported.
15+
16+
.. warning::
17+
18+
This component requires a fair amount of RAM; both for downloading the image, and for storing the decoded image. It might work on devices without PSRAM, but there is no guarantee.
19+
20+
This component has a dependency to :doc:`/components/http_request`; the configuration options you set to the ``http_request`` component will also apply here.
21+
22+
.. code-block:: yaml
23+
24+
online_image:
25+
- url: "https://example.com/example.png"
26+
format: png
27+
id: my_online_image
28+
29+
Configuration variables
30+
-----------------------
31+
32+
- **url** (**Required**, url): The URL where the image will be downloaded from.
33+
- **id** (**Required**, :ref:`config-id`): The ID with which you will be able to reference the image later
34+
in your display code.
35+
- **format** (**Required**): The format that the image is encoded with.
36+
37+
- ``PNG``: The image on the server is encoded in PNG format.
38+
- **resize** (*Optional*, string): If set, this will resize the image to fit inside the given dimensions ``WIDTHxHEIGHT``
39+
and preserve the aspect ratio.
40+
- **type** (*Optional*): Specifies how to encode image internally. Defaults to ``BINARY``.
41+
42+
- ``BINARY``: Two colors, suitable for 1 color displays or 2 color image in color displays. Uses 1 bit
43+
per pixel, 8 pixels per byte.
44+
- ``TRANSPARENT_BINARY``: One color, any pixel that is fully transparent will not be drawn, and any other pixel
45+
will be the on color. Uses 1 bit per pixel, 8 pixels per byte.
46+
- ``GRAYSCALE``: Full scale grey. Uses 8 bits per pixel, 1 pixel per byte.
47+
- ``RGB565``: Lossy RGB color stored. Uses 2 bytes per pixel.
48+
- ``RGB24``: Full RGB color stored. Uses 3 bytes per pixel.
49+
- ``RGBA``: Full RGB color stored. Uses 4 bytes per pixel. Any pixel with an alpha value < 127 will not be drawn.
50+
- **use_transparency** (*Optional*, boolean): If set the alpha channel of the input image will be taken into account,
51+
and pixels with alpha < 127 will not be drawn. For image types without explicit alpha channel,
52+
the color (0, 0, 1) (very dark blue) will be mapped to black, to be able to store transparency information
53+
within the image. Explicitly transparent types (``TRANSPARENT_BINARY`` and ``RGBA``) default to ``true`` and cannot be set to ``false``; other types default to ``false``.
54+
- **update_interval** (*Optional*, int): Redownload the image when the specified time has elapsed. Defaults to ``never`` (i.e. the update component action needs to be called manually).
55+
56+
Automations
57+
-----------
58+
59+
- **on_download_finished** (*Optional*, :ref:`Automation <automation>`): An automation to perform when the image has been successfully downloaded.
60+
61+
A good example for that is to update the display component after the download succeeded.
62+
63+
- **on_error** (*Optional*, :ref:`Automation <automation>`): An automation to perform when an error happened during download or decode.
64+
65+
Actions
66+
-------
67+
68+
**online_image.set_url**: Change the URL where the image is downloaded from. The image needs to be manually updated afterwards.
69+
70+
Configuration variables:
71+
72+
- **id** (**Required**, :ref:`config-id`): The image to update the URL for.
73+
- **url** (**Required**, url): The new URL to download the image from.
74+
75+
.. code-block:: yaml
76+
77+
on_...:
78+
- online_image.set_url:
79+
id: my_online_image
80+
url: "https://www.example.com/new_image.png"
81+
- component.update: my_online_image
82+
83+
**online_image.release**: Release the memory currently used by an image. Can be used if different display pages need different images, to avoid wasting memory on an image that is currently not being displayed.
84+
85+
Configuration variables:
86+
87+
- **id** (**Required**, :ref:`config-id`): The image to update the URL for.
88+
89+
.. code-block:: yaml
90+
91+
on_...:
92+
- online_image.release: my_online_image
93+
94+
Examples
95+
--------
96+
97+
.. code-block:: yaml
98+
99+
online_image:
100+
- url: "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png"
101+
format: png
102+
id: my_online_image
103+
on_download_finished:
104+
component.update: my_display
105+
106+
And then later in code:
107+
108+
.. code-block:: yaml
109+
110+
display:
111+
- platform: ...
112+
id: my_display
113+
# ...
114+
lambda: |-
115+
// Draw the image my_online_image at position [x=0,y=0]
116+
it.image(0, 0, id(my_online_image));
117+
118+
For monochrome displays the ``image`` method accepts two additional color parameters which can
119+
be supplied to specify the color used to draw bright and dark pixels respectively.
120+
In this case the image will be internally converted to a grayscale image and then to monochrome
121+
based on an internally defined threshold.
122+
123+
.. code-block:: yaml
124+
125+
display:
126+
- platform: ...
127+
id: my_display
128+
# ...
129+
lambda: |-
130+
// Draw the image my_image at position [x=0,y=0]
131+
// with front color "OFF" and back color "ON"
132+
it.image(0, 0, id(my_online_image), COLOR_OFF, COLOR_ON);
133+
134+
By default ``online_image`` is configured to not automatically update/download the image; in order to do the initial download, you can either:
135+
- Add a ``component.update <image_id>`` in the ``on_connect:`` action on the :doc:`/components/wifi` component.
136+
- Explicitly set an ``update_interval``.
137+
- Call ``component.update <image_id>`` in an :doc:`/components/interval` block.
138+
- Call ``component.update <image_id>`` where you need the image to be downloaded/updated.
139+
140+
.. code-block:: yaml
141+
142+
wifi:
143+
on_connect:
144+
- component.update: my_online_image
145+
146+
See Also
147+
--------
148+
149+
- :apiref:`online_image/online_image.h`
150+
- :doc:`Image Component <image>`
151+
- :doc:`Animation Component <animation>`
152+
- :ghedit:`Edit`

images/image-sync-outline.svg

Lines changed: 1 addition & 0 deletions
Loading

index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ Display Components
823823
QR Code, components/qr_code, qr-code.svg, dark-invert
824824
Image, components/image, image-outline.svg, dark-invert
825825
Animation, components/animation, image-multiple-outline.svg, dark-invert
826+
Online Image, components/online_image, image-sync-outline.svg, dark-invert
826827
Display Menu Core, components/display_menu/index, folder-open.svg, dark-invert
827828
Graphical Display Menu, components/display_menu/graphical_display_menu, graphical_display_menu.png
828829
LCD Menu, components/display_menu/lcd_menu, lcd_menu.png

0 commit comments

Comments
 (0)