mirror of
https://github.com/esphome/esphome.git
synced 2024-11-14 11:08:10 +01:00
Remove dependency on libmagic
This commit is contained in:
parent
458a8970b6
commit
659370e556
1 changed files with 13 additions and 17 deletions
|
@ -1,18 +1,17 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import hashlib
|
||||
import io
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import re
|
||||
from magic import Magic
|
||||
|
||||
from esphome import core
|
||||
from esphome.components import font
|
||||
from esphome import external_files
|
||||
import esphome.config_validation as cv
|
||||
from PIL import UnidentifiedImageError
|
||||
|
||||
from esphome import core, external_files
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import font
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_DITHER,
|
||||
CONF_FILE,
|
||||
|
@ -30,7 +29,6 @@ from esphome.core import CORE, HexInt
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "image"
|
||||
DEPENDENCIES = ["display"]
|
||||
MULTI_CONF = True
|
||||
MULTI_CONF_NO_DEFAULT = True
|
||||
|
||||
|
@ -239,12 +237,11 @@ CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, IMAGE_SCHEMA)
|
|||
|
||||
def load_svg_image(file: bytes, resize: tuple[int, int]):
|
||||
# Local import only to allow "validate_pillow_installed" to run *before* importing it
|
||||
from PIL import Image
|
||||
|
||||
# This import is only needed in case of SVG images; adding it
|
||||
# to the top would force configurations not using SVG to also have it
|
||||
# installed for no reason.
|
||||
from cairosvg import svg2png
|
||||
from PIL import Image
|
||||
|
||||
if resize:
|
||||
req_width, req_height = resize
|
||||
|
@ -280,17 +277,16 @@ async def to_code(config):
|
|||
except Exception as e:
|
||||
raise core.EsphomeError(f"Could not load image file {path}: {e}")
|
||||
|
||||
mime = Magic(mime=True)
|
||||
file_type = mime.from_buffer(file_contents)
|
||||
|
||||
resize = config.get(CONF_RESIZE)
|
||||
if "svg" in file_type:
|
||||
image = load_svg_image(file_contents, resize)
|
||||
else:
|
||||
try:
|
||||
image = Image.open(io.BytesIO(file_contents))
|
||||
if resize:
|
||||
image.thumbnail(resize)
|
||||
|
||||
except UnidentifiedImageError as exc:
|
||||
if "<svg" in str(file_contents):
|
||||
image = load_svg_image(file_contents, resize)
|
||||
else:
|
||||
raise core.EsphomeError(f"Could not load image file {path}") from exc
|
||||
width, height = image.size
|
||||
|
||||
if CONF_RESIZE not in config and (width > 500 or height > 500):
|
||||
|
|
Loading…
Reference in a new issue