Optimize generated gifs.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle 2024-12-15 11:30:23 +01:00
parent 8651884ca6
commit 2c25b33bcc
5 changed files with 35 additions and 6 deletions

15
poetry.lock generated
View File

@ -1000,6 +1000,19 @@ files = [
{file = "pycparser-2.22.tar.gz", hash = "sha256:491c8be9c040f5390f5bf44a5b07752bd07f56edf992381b05c701439eec10f6"}, {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]] [[package]]
name = "pygments" name = "pygments"
version = "2.18.0" version = "2.18.0"
@ -1490,4 +1503,4 @@ files = [
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "e543bbcb3ca374c270392aeef932dd1711e54689f73923e90302a28822a243fe" content-hash = "1b586d7f23aca41e499ce94ec10eacfab3c2e260bb16822638ae1f831b2988be"

View File

@ -16,6 +16,7 @@ sympy = "^1.13.3"
networkx = "^3.4.2" networkx = "^3.4.2"
pillow = "^11.0.0" pillow = "^11.0.0"
imageio = "^2.36.1" imageio = "^2.36.1"
pygifsicle = "^1.1.0"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
pyright = "^1.1.389" pyright = "^1.1.389"

View File

@ -1,7 +1,16 @@
from abc import abstractmethod from abc import abstractmethod
from logging import Logger from logging import Logger
from pathlib import Path 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 from numpy.typing import NDArray
@ -18,15 +27,19 @@ class ProgressHandler(Protocol):
class FileHandler: class FileHandler:
@abstractmethod @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]): def image(self, filename: str, image: NDArray[Any]):
from io import BytesIO
import imageio.v3 as iio import imageio.v3 as iio
from pygifsicle import optimize # type: ignore
data = iio.imwrite("<bytes>", image, extension=Path(filename).suffix) # type: ignore data = iio.imwrite("<bytes>", 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: class BaseSolver:

View File

@ -14,3 +14,4 @@ class FileHandlerAPI(FileHandler):
with open(self.folder.joinpath(filename), "wb") as fp: with open(self.folder.joinpath(filename), "wb") as fp:
fp.write(content) fp.write(content)
dump_api_message("file", {"filename": filename, "size": len(content)}) dump_api_message("file", {"filename": filename, "size": len(content)})
return self.folder.joinpath(filename)

View File

@ -18,3 +18,4 @@ class SimpleFileHandler(FileHandler):
self.folder.mkdir(exist_ok=True) self.folder.mkdir(exist_ok=True)
with open(self.folder.joinpath(filename), "wb") as fp: with open(self.folder.joinpath(filename), "wb") as fp:
fp.write(content) fp.write(content)
return self.folder.joinpath(filename)