Skip to content

Commit

Permalink
fix: resize gifs and copy, add pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
eelucio committed May 28, 2024
1 parent b2fd2b7 commit 9b75aea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
30 changes: 26 additions & 4 deletions src/evaluation_system/api/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from typing import Any, Dict, Iterator, Optional, Sequence, Tuple, TypeVar, Union, cast

from django.db.models.query import QuerySet
from PIL import Image
from PIL import Image, ImageSequence
from rich import print as pprint
from typing_extensions import TypedDict

Expand Down Expand Up @@ -609,11 +609,33 @@ def _preview_convert(source_path: str, dest_path: str, width: int = 800) -> None
so height is calculated automatically. Default is 800
"""

with Image.open(source_path) as img:
def resizer(img, width):
x, y = img.size
height = 2 * (int(y / (x / width)) // 2)
im_resize = img.resize((width, height))
im_resize.save(dest_path)
return img.resize((width, height))

with Image.open(source_path) as img:
if img.format == "GIF":
frames = []
duration = []
for frame in ImageSequence.Iterator(img):
im_resize = resizer(frame, width)
frames.append(im_resize)
duration.append(frame.info.get("duration", 0))
frames[0].save(
dest_path,
format="GIF",
save_all=True,
append_images=frames[1:],
loop=0,
duration=duration,
optimize=True,
quality=95,
)
else:
im_resize = resizer(img, width)
im_resize.save(dest_path)

os.chmod(dest_path, 509)


Expand Down
5 changes: 5 additions & 0 deletions src/evaluation_system/tests/plugin_manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ def test_preview_generation(dummy_env):
s = os.path.dirname(__file__) + "/test_output/test_image.png"
pm._preview_convert(s, d)
assert os.path.isfile(d)
with tempfile.TemporaryDirectory() as td:
d = str(Path(td) / "tmp.gif")
s = os.path.dirname(__file__) + "/test_output/test_animation.gif"
pm._preview_convert(s, d)
assert os.path.isfile(d)

r = pm._preview_generate_name("dummy", {})
assert "dummy" in r
Expand Down
2 changes: 1 addition & 1 deletion src/evaluation_system/tests/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ def test_prepare_output(dummy_plugin):
assert meta_data["type"] == check["type"]
assert meta_data["caption"] == "Manually added result"
meta_data = plugin._run_tool({"folder": str(fn.parent)})
assert len(meta_data) == 2
assert len(meta_data) == 3


@mock.patch("os.getpid", lambda: 12345)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9b75aea

Please sign in to comment.