Unverified Commit 558e96d9 authored by narugo1992's avatar narugo1992 Committed by GitHub
Browse files

Merge pull request #103 from deepghs/fix/gif

fix(narugo): fix metadata reading for png format, raise error when comment info is not bytes
parents c3e2249a a7a8b3ef
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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
+12 −0
Original line number Diff line number Diff line
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, \
@@ -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):
@@ -165,3 +171,9 @@ class TestMetadataGeninfo:
        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
+1.02 MiB
Loading image diff...