From 699d00e21850c813e3f995c7d4fec0411cd58ef7 Mon Sep 17 00:00:00 2001 From: guillempages Date: Wed, 12 Jun 2024 03:11:00 +0200 Subject: [PATCH] [image] Make PIL import local (#6864) --- esphome/components/image/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/esphome/components/image/__init__.py b/esphome/components/image/__init__.py index b23ed3445a..c275136427 100644 --- a/esphome/components/image/__init__.py +++ b/esphome/components/image/__init__.py @@ -9,8 +9,6 @@ import re import requests from magic import Magic -from PIL import Image - from esphome import core from esphome.components import font from esphome import external_files @@ -267,6 +265,9 @@ 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. @@ -286,6 +287,9 @@ def load_svg_image(file: bytes, resize: tuple[int, int]): async def to_code(config): + # Local import only to allow "validate_pillow_installed" to run *before* importing it + from PIL import Image + conf_file = config[CONF_FILE] if conf_file[CONF_SOURCE] == SOURCE_LOCAL: