mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Add SVG image support (#4922)
This commit is contained in:
parent
5f0892dec4
commit
c74105aad7
1 changed files with 32 additions and 21 deletions
|
@ -199,6 +199,27 @@ IMAGE_SCHEMA = cv.Schema(
|
||||||
CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, IMAGE_SCHEMA)
|
CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, IMAGE_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
|
def load_svg_image(file: str, resize: tuple[int, int]):
|
||||||
|
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
|
||||||
|
|
||||||
|
if resize:
|
||||||
|
req_width, req_height = resize
|
||||||
|
svg_image = svg2png(
|
||||||
|
url=file,
|
||||||
|
output_width=req_width,
|
||||||
|
output_height=req_height,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
svg_image = svg2png(url=file)
|
||||||
|
|
||||||
|
return Image.open(io.BytesIO(svg_image))
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
@ -206,30 +227,20 @@ async def to_code(config):
|
||||||
|
|
||||||
if conf_file[CONF_SOURCE] == SOURCE_LOCAL:
|
if conf_file[CONF_SOURCE] == SOURCE_LOCAL:
|
||||||
path = CORE.relative_config_path(conf_file[CONF_PATH])
|
path = CORE.relative_config_path(conf_file[CONF_PATH])
|
||||||
try:
|
|
||||||
image = Image.open(path)
|
|
||||||
except Exception as e:
|
|
||||||
raise core.EsphomeError(f"Could not load image file {path}: {e}")
|
|
||||||
if CONF_RESIZE in config:
|
|
||||||
image.thumbnail(config[CONF_RESIZE])
|
|
||||||
elif conf_file[CONF_SOURCE] == SOURCE_MDI:
|
elif conf_file[CONF_SOURCE] == SOURCE_MDI:
|
||||||
# Those imports are only needed in case of MDI images; adding them
|
path = _compute_local_icon_path(conf_file).as_posix()
|
||||||
# to the top would force configurations not using MDI to also have them
|
|
||||||
# installed for no reason.
|
|
||||||
from cairosvg import svg2png
|
|
||||||
|
|
||||||
svg_file = _compute_local_icon_path(conf_file)
|
try:
|
||||||
if CONF_RESIZE in config:
|
resize = config.get(CONF_RESIZE)
|
||||||
req_width, req_height = config[CONF_RESIZE]
|
if path.lower().endswith(".svg"):
|
||||||
svg_image = svg2png(
|
image = load_svg_image(path, resize)
|
||||||
url=svg_file.as_posix(),
|
|
||||||
output_width=req_width,
|
|
||||||
output_height=req_height,
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
svg_image = svg2png(url=svg_file.as_posix())
|
image = Image.open(path)
|
||||||
|
if resize:
|
||||||
image = Image.open(io.BytesIO(svg_image))
|
image.thumbnail(resize)
|
||||||
|
except Exception as e:
|
||||||
|
raise core.EsphomeError(f"Could not load image file {path}: {e}")
|
||||||
|
|
||||||
width, height = image.size
|
width, height = image.size
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue