Skip to content

Commit

Permalink
[lib] Changed ImageIterator's caching criteria
Browse files Browse the repository at this point in the history
- Change: `ImageIterator` now uses image file size instead of frame count.

NOTE: This change is temporary and will be reverted as soon as a Pillow version including the improvements in python-pillow/Pillow#6077 is released.
  • Loading branch information
AnonymouX47 committed Mar 10, 2022
1 parent f6c0555 commit 7362440
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions term_img/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,11 +1355,12 @@ class ImageIterator:
of subsequent renders) or not.
- If a ``bool``, it directly sets if the frames will be cached or not.
- If an ``int``, caching is enabled only if the number of frames in the image
is less than or equal to the given number.
- If *repeat* equals ``1``, caching is disabled.
- If an ``int``, caching is enabled only if the size of the image file
- in bytes - is less than or equal to the given number or *image* was
instantiated with a PIL image.
NOTE:
- If *repeat* equals ``1``, caching is disabled.
- The iterator has immediate response to changes in the image
:term:`render size` and :term:`scale`.
- If the :term:`render size` is :ref:`unset <unset-size>`, it's automatically
Expand All @@ -1373,7 +1374,7 @@ def __init__(
image: TermImage,
repeat: int = -1,
format: str = "",
cached: Union[bool, int] = 100,
cached: Union[bool, int] = 2 * 2 ** 20,
):
if not isinstance(image, TermImage):
raise TypeError(f"Invalid type for 'image' (got: {type(image).__name__})")
Expand All @@ -1397,8 +1398,13 @@ def __init__(
self._image = image
self._repeat = repeat
self._format = format
size = (
0
if isinstance(image._source, Image.Image)
else os.stat(image._source).st_size
)
self._cached = (
cached if isinstance(cached, bool) else image.n_frames <= cached
cached if isinstance(cached, bool) else size <= cached
) and repeat != 1
self._animator = image._renderer(self._animate, alpha, fmt, check_size=False)

Expand Down

0 comments on commit 7362440

Please sign in to comment.