PiotrMachowski / Home-Assistant-custom-components-Xiaomi-Cloud-Map-Extractor

This custom integration provides a way to present a live view of a map for Xiaomi (Roborock/Viomi/Roidmi/Dreame) vacuums without a need for rooting.
MIT License
1.15k stars 122 forks source link

Warning: Unable to open font (...) #20

Open leva3000 opened 4 years ago

leva3000 commented 4 years ago

I cannot add label in Cyrillic, such as "Кухня":

Traceback (most recent call last):
  File "E:\HassWP\python-3.7.7\lib\site-packages\homeassistant\helpers\entity.py", line 279, in async_update_ha_state
    await self.async_device_update()
  File "E:\HassWP\python-3.7.7\lib\site-packages\homeassistant\helpers\entity.py", line 472, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "E:\HassWP\python-3.7.7\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "E:\HassWP\config\custom_components\xiaomi_cloud_map_extractor\camera.py", line 186, in update
    self._image_config)
  File "E:\HassWP\config\custom_components\xiaomi_cloud_map_extractor\xiaomi_cloud_connector.py", line 105, in get_map
    map_data = MapDataParser.parse(unzipped, colors, drawables, texts, sizes, image_config)
  File "E:\HassWP\config\custom_components\xiaomi_cloud_map_extractor\map_data_parser.py", line 76, in parse
    MapDataParser.draw_elements(colors, drawables, texts, sizes, map_data)
  File "E:\HassWP\config\custom_components\xiaomi_cloud_map_extractor\map_data_parser.py", line 237, in draw_elements
    ImageHandler.draw_texts(map_data.image, texts)
  File "E:\HassWP\config\custom_components\xiaomi_cloud_map_extractor\image_handler.py", line 171, in draw_texts
    text_config[CONF_FONT], text_config[CONF_FONT_SIZE])
  File "E:\HassWP\config\custom_components\xiaomi_cloud_map_extractor\image_handler.py", line 206, in __draw_text__
    w, h = draw.textsize(text, font)
  File "E:\HassWP\config\deps\Python37\site-packages\PIL\ImageDraw.py", line 430, in textsize
    return font.getsize(text, direction, features, language, stroke_width)
  File "E:\HassWP\config\deps\Python37\site-packages\PIL\ImageFont.py", line 128, in getsize
    return self.font.getsize(text)
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-6: ordinal not in range(256)
PiotrMachowski commented 4 years ago

It's impossible using default font, but when you use a font that supports latin characters everything works: image

leva3000 commented 4 years ago

Thanks, just need to know all of my font available. I am using HA in docker.

PiotrMachowski commented 4 years ago

Here you have a command that should retrieve all available fonts: https://github.com/PiotrMachowski/Home-Assistant-custom-components-Xiaomi-Cloud-Map-Extractor#texts-configuration

leva3000 commented 4 years ago

On RPi I have another error:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 278, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 471, in async_device_update
    await self.hass.async_add_executor_job(self.update)  # type: ignore
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/xiaomi_cloud_map_extractor/camera.py", line 185, in update
    self._map_data = self._connector.get_map(map_name, self._colors, self._drawables, self._texts, self._sizes,
  File "/config/custom_components/xiaomi_cloud_map_extractor/xiaomi_cloud_connector.py", line 105, in get_map
    map_data = MapDataParser.parse(unzipped, colors, drawables, texts, sizes, image_config)
  File "/config/custom_components/xiaomi_cloud_map_extractor/map_data_parser.py", line 76, in parse
    MapDataParser.draw_elements(colors, drawables, texts, sizes, map_data)
  File "/config/custom_components/xiaomi_cloud_map_extractor/map_data_parser.py", line 237, in draw_elements
    ImageHandler.draw_texts(map_data.image, texts)
  File "/config/custom_components/xiaomi_cloud_map_extractor/image_handler.py", line 170, in draw_texts
    ImageHandler.__draw_text__(image, text_config[CONF_TEXT], x, y, text_config[CONF_COLOR],
  File "/config/custom_components/xiaomi_cloud_map_extractor/image_handler.py", line 205, in __draw_text__
    font = ImageFont.truetype(font_file, font_size)
  File "/usr/local/lib/python3.8/site-packages/PIL/ImageFont.py", line 655, in truetype
    return freetype(font)
  File "/usr/local/lib/python3.8/site-packages/PIL/ImageFont.py", line 652, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/local/lib/python3.8/site-packages/PIL/ImageFont.py", line 169, in __init__
    if core.HAVE_RAQM:
  File "/usr/local/lib/python3.8/site-packages/PIL/ImageFont.py", line 43, in __getattr__
    raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed
PiotrMachowski commented 4 years ago

Can you check which version of pillow/PIL you have?

pip3 list
leva3000 commented 4 years ago

I am trying to figure out how deps are installed for HA in docker... Which python is used and where to run the command.

leva3000 commented 4 years ago

Pillow 7.2.0

leva3000 commented 4 years ago

I am trying to run the next:

apk add --no-cache g++ freetype-dev jpeg-dev
pip uninstall pillow
pip install --no-cache-dir pillow

to make sure pillow will be compiled with libfreetype. I am not sure if this will be a persistent solution for HA in docker.

leva3000 commented 4 years ago

It looks like no fonts preinstalled in homeassistant docker image

PiotrMachowski commented 4 years ago

I will try to find a way to attach a font file

leva3000 commented 4 years ago

Solution for me was to jump inside HA container and run the next commands:

apk add --no-cache g++ freetype-dev jpeg-dev
pip uninstall pillow
pip install --no-cache-dir pillow
apk add font-noto

and use NotoSansDisplay-Light.ttf font.

BTW it will be nice to choose TTF file stored somewhere in the config folder.

leva3000 commented 4 years ago

vacuum_fonts_fix

PiotrMachowski commented 4 years ago

Using TTF stored in a config is much better, as it doesn't require manual changes after every HA update. I will check if it is possible

leva3000 commented 4 years ago

But there is still an issue with Pillow installed(compiled during installation) without libfreetype. I don't know if there is any solution for that. That how homeassistant processing requirements, looks like no options to control that. So I still have to run the command after every update of HA.

8osman commented 4 years ago

I've hit the same problem, unfortunately my understanding is too limited to follow the instructions above.

I'm left with "ImportError: The _imagingft C module is not installed"

My home assistant is also installed within docker.

Thanks for the great work, really looking forward to being able to utilise this! :-)

leva3000 commented 4 years ago

After HA was updated to 0.116.2 I cannot fix the issue with the above commands. Every time I see The _imagingft C module is not installed error.

XxCabbyoO commented 3 years ago

After HA was updated to 0.116.2 I cannot fix the issue with the above commands. Every time I see The _imagingft C module is not installed error.

I have the same error. So there is currently no solution to fix the Problem?

RPI4

PiotrMachowski commented 3 years ago

Unfortunately I haven't found a solution yet

PiotrMachowski commented 3 years ago

I have added handling to errors thrown during fonts loading.

Nicols07 commented 3 years ago

I use HA on Docker with my NAS Synology. No fonts TTF on Synology...

it's Ok wihout font: "FreeSans.ttf" but i can't change the font_size...

texts:
      - text: "Room 1"
        x: 25
        y: 25
        color: [125, 20, 213]
        font_size: 100
      - text: "Room 2"
        x: 25
        y: 75
        color: [125, 20, 213, 127]
        font_size: 10

image

PiotrMachowski commented 3 years ago

@Nicols07 unfortunately it's not possible to change size for a default font

tmjo commented 2 years ago

Hi! I came over this issue when troubleshooting a similar issue with one of my own integrations. Not sure if it helps for this case, but I found a workaround by using bitmap fonts and then converting to PIL-fonts. Quite cumbersome solution, but it works better in my case. More info here if you're interested.