From 2c25b33bcc2b45b43c5502a6573ccf74c25bb446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sun, 15 Dec 2024 11:30:23 +0100 Subject: [PATCH] Optimize generated gifs. --- poetry.lock | 15 ++++++++++++++- pyproject.toml | 1 + src/holt59/aoc/base.py | 23 ++++++++++++++++++----- src/holt59/aoc/utils/api/files.py | 1 + src/holt59/aoc/utils/files.py | 1 + 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/poetry.lock b/poetry.lock index 2cafd42..9f84788 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1000,6 +1000,19 @@ files = [ {file = "pycparser-2.22.tar.gz", hash = "sha256:491c8be9c040f5390f5bf44a5b07752bd07f56edf992381b05c701439eec10f6"}, ] +[[package]] +name = "pygifsicle" +version = "1.1.0" +description = "Python package wrapping the gifsicle library for editing and optimizing gifs." +optional = false +python-versions = "*" +files = [ + {file = "pygifsicle-1.1.0.tar.gz", hash = "sha256:dcef433520ace4c1136dfc7060e77042142a3dbd6bdb6a19bd9149ef5cbe7441"}, +] + +[package.extras] +test = ["pytest", "pytest-cov", "touch", "validate_version_code"] + [[package]] name = "pygments" version = "2.18.0" @@ -1490,4 +1503,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "e543bbcb3ca374c270392aeef932dd1711e54689f73923e90302a28822a243fe" +content-hash = "1b586d7f23aca41e499ce94ec10eacfab3c2e260bb16822638ae1f831b2988be" diff --git a/pyproject.toml b/pyproject.toml index b73e8ea..aa8044c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ sympy = "^1.13.3" networkx = "^3.4.2" pillow = "^11.0.0" imageio = "^2.36.1" +pygifsicle = "^1.1.0" [tool.poetry.group.dev.dependencies] pyright = "^1.1.389" diff --git a/src/holt59/aoc/base.py b/src/holt59/aoc/base.py index 7b66d63..17002f7 100644 --- a/src/holt59/aoc/base.py +++ b/src/holt59/aoc/base.py @@ -1,7 +1,16 @@ from abc import abstractmethod from logging import Logger from pathlib import Path -from typing import Any, Final, Iterable, Iterator, Protocol, Sequence, TypeVar, overload +from typing import ( + Any, + Final, + Iterable, + Iterator, + Protocol, + Sequence, + TypeVar, + overload, +) from numpy.typing import NDArray @@ -18,15 +27,19 @@ class ProgressHandler(Protocol): class FileHandler: @abstractmethod - def create(self, filename: str, content: bytes, text: bool = False): ... + def create( + self, filename: str, content: bytes, text: bool = False + ) -> Path | None: ... def image(self, filename: str, image: NDArray[Any]): - from io import BytesIO - import imageio.v3 as iio + from pygifsicle import optimize # type: ignore data = iio.imwrite("", image, extension=Path(filename).suffix) # type: ignore - self.create(filename, data, False) + path = self.create(filename, data, False) + + assert path is not None + optimize(path, options=["--no-warnings"]) class BaseSolver: diff --git a/src/holt59/aoc/utils/api/files.py b/src/holt59/aoc/utils/api/files.py index 3d6650a..b8f9447 100644 --- a/src/holt59/aoc/utils/api/files.py +++ b/src/holt59/aoc/utils/api/files.py @@ -14,3 +14,4 @@ class FileHandlerAPI(FileHandler): with open(self.folder.joinpath(filename), "wb") as fp: fp.write(content) dump_api_message("file", {"filename": filename, "size": len(content)}) + return self.folder.joinpath(filename) diff --git a/src/holt59/aoc/utils/files.py b/src/holt59/aoc/utils/files.py index b7148ee..097cf0e 100644 --- a/src/holt59/aoc/utils/files.py +++ b/src/holt59/aoc/utils/files.py @@ -18,3 +18,4 @@ class SimpleFileHandler(FileHandler): self.folder.mkdir(exist_ok=True) with open(self.folder.joinpath(filename), "wb") as fp: fp.write(content) + return self.folder.joinpath(filename)