Skip to content

Commit

Permalink
Merge pull request #103 from deepghs/fix/gif
Browse files Browse the repository at this point in the history
fix(narugo): fix metadata reading for png format, raise error when comment info is not bytes
  • Loading branch information
narugo1992 authored Sep 10, 2024
2 parents c3e2249 + a7a8b3e commit 558e96d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion imgutils/metadata/geninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def read_geninfo_gif(image: ImageTyping) -> Optional[str]:
"""
image = load_image(image, mode=None, force_background=None)
infos = image.info or {}
if "comment" in infos: # for gif
if "comment" in infos and isinstance(infos['comment'], (bytes, bytearray)): # for gif
return infos["comment"].decode("utf8", errors="ignore")
else:
return None
Expand Down
12 changes: 12 additions & 0 deletions test/metadata/test_geninfo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from PIL import Image
from hbutils.testing import tmatrix, isolated_directory

from imgutils.metadata import read_geninfo_parameters, read_geninfo_exif, read_geninfo_gif, write_geninfo_parameters, \
Expand Down Expand Up @@ -26,6 +27,11 @@ def png_rgb_clean():
return get_testfile('nai3_clear.png')


@pytest.fixture()
def png_comment_str():
return get_testfile('nai3_clear_comment_str.png')


@pytest.mark.unittest
class TestMetadataGeninfo:
def test_read_geninfo_parameters(self, a41_webui_file):
Expand Down Expand Up @@ -165,3 +171,9 @@ def test_write_geninfo_gif(self, ext, text, png_rgb_clean):
with isolated_directory():
write_geninfo_gif(png_rgb_clean, f'image{ext}', geninfo=text)
assert read_geninfo_gif(f'image{ext}') == (text if text else None)

def test_read_geninfo_gif_comment_str(self, png_comment_str):
image = Image.open(png_comment_str)
assert isinstance(image.info.get('comment'), str)
assert read_geninfo_gif(png_comment_str) is None
assert read_geninfo_gif(image) is None
Binary file added test/testfile/nai3_clear_comment_str.png
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 558e96d

Please sign in to comment.