Skip to content

Commit

Permalink
black and setup stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhumber committed Mar 9, 2023
1 parent 3c088e3 commit c34a3cf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
pytest
pytest tests

format:
isort .
Expand Down
29 changes: 13 additions & 16 deletions gif.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand All @@ -115,5 +112,5 @@ def save(
duration=duration,
disposal=0 if overlapping else 2,
loop=0,
**kwargs
**kwargs,
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions tests/test_gif.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

import pytest
from matplotlib import pyplot as plt
from PIL import Image
Expand Down

0 comments on commit c34a3cf

Please sign in to comment.