From c34a3cf6d85de398a7d3a0a77379a8e6f2067ce9 Mon Sep 17 00:00:00 2001 From: Max Humber Date: Thu, 9 Mar 2023 11:34:32 -0500 Subject: [PATCH] black and setup stuff --- Makefile | 2 +- gif.py | 29 +++++++++++++---------------- setup.py | 2 +- tests/test_gif.py | 1 + 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 46ac9c4..8394a8f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ test: - pytest + pytest tests format: isort . diff --git a/gif.py b/gif.py index efd90f1..e84bf84 100644 --- a/gif.py +++ b/gif.py @@ -1,11 +1,13 @@ from functools import wraps from io import BytesIO as Buffer -from typing import Callable, List, TypeVar +from typing import Callable, List, TypeVar, ByteString from matplotlib import pyplot as plt +from numpy import array as numpy_array +from numpy import vsplit as numpy_vsplit +from numpy import vstack as numpy_vstack from PIL import Image as PI - Plot = TypeVar("Plot") Frame = PI.Image Milliseconds = float @@ -60,18 +62,16 @@ def inner(*args, **kwargs) -> Frame: # type: ignore[valid-type] return inner -def _optimize_frames(frames: List[Frame]): - import numpy as np - joined_img = PI.fromarray(np.vstack(frames)) - joined_img = joined_img.quantize(colors=256 - 1, dither=0) - palette = b'\xff\x00\xff' + joined_img.palette.getdata()[1] - joined_img_arr = np.array(joined_img) +def _optimize_frames(frames: List[Frame]) -> (List[PI.Image], ByteString): # type: ignore[valid-type] + joined_img = PI.fromarray(numpy_vstack(frames)) + joined_img = joined_img.quantize(colors=255, dither=0) + palette = b"\xff\x00\xff" + joined_img.palette.getdata()[1] + joined_img_arr = numpy_array(joined_img) joined_img_arr += 1 - arrays = np.vsplit(joined_img_arr, len(frames)) - + arrays = numpy_vsplit(joined_img_arr, len(frames)) prev_array = arrays[0] for array in arrays[1:]: - mask = array == prev_array + mask = (array == prev_array) prev_array = array.copy() array[mask] = 0 frames_out = [PI.fromarray(array) for array in arrays] @@ -102,10 +102,7 @@ def save( kwargs = {} if optimize: frames, palette = _optimize_frames(frames) - kwargs = { - "palette": palette, - "transparency": 0, - } + kwargs = {"palette": palette, "transparency": 0} frames[0].save( # type: ignore path, @@ -115,5 +112,5 @@ def save( duration=duration, disposal=0 if overlapping else 2, loop=0, - **kwargs + **kwargs, ) diff --git a/setup.py b/setup.py index 9927e4b..5d50b5f 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="gif", - version="22.11.0", + version="23.03.0", description="The matplotlib Animation Extension", long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_gif.py b/tests/test_gif.py index 5d260a7..b5976bd 100644 --- a/tests/test_gif.py +++ b/tests/test_gif.py @@ -1,4 +1,5 @@ import os + import pytest from matplotlib import pyplot as plt from PIL import Image