diff --git a/esphome/components/animation/__init__.py b/esphome/components/animation/__init__.py index 1780bdf72e..7c9ff07f97 100644 --- a/esphome/components/animation/__init__.py +++ b/esphome/components/animation/__init__.py @@ -44,8 +44,9 @@ async def to_code(config): width, height = image.size frames = image.n_frames if CONF_RESIZE in config: - image.thumbnail(config[CONF_RESIZE]) - width, height = image.size + new_width_max, new_height_max = config[CONF_RESIZE] + ratio = min(new_width_max / width, new_height_max / height) + width, height = int(width * ratio), int(height * ratio) else: if width > 500 or height > 500: _LOGGER.warning( @@ -59,10 +60,12 @@ async def to_code(config): for frameIndex in range(frames): image.seek(frameIndex) frame = image.convert("L", dither=Image.NONE) + if CONF_RESIZE in config: + frame = frame.resize([width, height]) pixels = list(frame.getdata()) if len(pixels) != height * width: raise core.EsphomeError( - f"Unexpected number of pixels in frame {frameIndex}: {len(pixels)} != {height*width}" + f"Unexpected number of pixels in {path} frame {frameIndex}: ({len(pixels)} != {height*width})" ) for pix in pixels: data[pos] = pix @@ -73,13 +76,13 @@ async def to_code(config): pos = 0 for frameIndex in range(frames): image.seek(frameIndex) - if CONF_RESIZE in config: - image.thumbnail(config[CONF_RESIZE]) frame = image.convert("RGB") + if CONF_RESIZE in config: + frame = frame.resize([width, height]) pixels = list(frame.getdata()) if len(pixels) != height * width: raise core.EsphomeError( - f"Unexpected number of pixels in frame {frameIndex}: {len(pixels)} != {height*width}" + f"Unexpected number of pixels in {path} frame {frameIndex}: ({len(pixels)} != {height*width})" ) for pix in pixels: data[pos] = pix[0] @@ -95,6 +98,8 @@ async def to_code(config): for frameIndex in range(frames): image.seek(frameIndex) frame = image.convert("1", dither=Image.NONE) + if CONF_RESIZE in config: + frame = frame.resize([width, height]) for y in range(height): for x in range(width): if frame.getpixel((x, y)):