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

View File

@@ -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("<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:

View File

@@ -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)

View File

@@ -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)