From 0bbdb6a486502adb588578da7423e74bd6dfa239 Mon Sep 17 00:00:00 2001 From: brandon Date: Tue, 3 Dec 2024 21:32:52 +0000 Subject: [PATCH 1/4] remove imghdr --- src/groundlight/images.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/groundlight/images.py b/src/groundlight/images.py index 695968a1..1dd48d45 100644 --- a/src/groundlight/images.py +++ b/src/groundlight/images.py @@ -1,5 +1,5 @@ # pylint: disable=deprecated-module -import imghdr +from pathlib import Path from io import BufferedReader, BytesIO, IOBase from typing import Union @@ -30,17 +30,17 @@ def getvalue(self) -> bytes: def close(self) -> None: pass - def bytestream_from_filename(image_filename: str, jpeg_quality: int = DEFAULT_JPEG_QUALITY) -> ByteStreamWrapper: """Determines what to do with an arbitrary filename Only supports JPEG and PNG files for now. For PNG files, we convert to RGB format used in JPEGs. """ - if imghdr.what(image_filename) == "jpeg": + image_path = Path(image_filename) + if image_path.suffix == "jpeg": buffer = buffer_from_jpeg_file(image_filename) return ByteStreamWrapper(data=buffer) - if imghdr.what(image_filename) == "png": + if image_path.suffix == "png": pil_img = Image.open(image_filename) # This chops off the alpha channel which can cause unexpected behavior, but handles minimal transparency well pil_img = pil_img.convert("RGB") @@ -53,7 +53,7 @@ def buffer_from_jpeg_file(image_filename: str) -> BufferedReader: For now, we only support JPEG files, and raise an ValueError otherwise. """ - if imghdr.what(image_filename) == "jpeg": + if Path(image_filename).suffix == "jpeg": # Note this will get fooled by truncated binaries since it only reads the header. # That's okay - the server will catch it. return open(image_filename, "rb") From d5d5f42f29efdf593a6f1cc3c2e6f78eb85928fe Mon Sep 17 00:00:00 2001 From: Auto-format Bot Date: Tue, 3 Dec 2024 21:33:52 +0000 Subject: [PATCH 2/4] Automatically reformatting code --- src/groundlight/images.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/groundlight/images.py b/src/groundlight/images.py index 1dd48d45..7462a206 100644 --- a/src/groundlight/images.py +++ b/src/groundlight/images.py @@ -1,6 +1,6 @@ # pylint: disable=deprecated-module -from pathlib import Path from io import BufferedReader, BytesIO, IOBase +from pathlib import Path from typing import Union from groundlight.optional_imports import Image, np @@ -30,6 +30,7 @@ def getvalue(self) -> bytes: def close(self) -> None: pass + def bytestream_from_filename(image_filename: str, jpeg_quality: int = DEFAULT_JPEG_QUALITY) -> ByteStreamWrapper: """Determines what to do with an arbitrary filename From a5d1dbbdbed9a8f60e6fb34e7917f36f39cd4695 Mon Sep 17 00:00:00 2001 From: brandon Date: Tue, 3 Dec 2024 21:45:23 +0000 Subject: [PATCH 3/4] corrections --- src/groundlight/images.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/groundlight/images.py b/src/groundlight/images.py index 1dd48d45..3b7dfac0 100644 --- a/src/groundlight/images.py +++ b/src/groundlight/images.py @@ -37,10 +37,10 @@ def bytestream_from_filename(image_filename: str, jpeg_quality: int = DEFAULT_JP For PNG files, we convert to RGB format used in JPEGs. """ image_path = Path(image_filename) - if image_path.suffix == "jpeg": + if image_path.suffix.lower() in (".jpeg", ".jpg"): buffer = buffer_from_jpeg_file(image_filename) return ByteStreamWrapper(data=buffer) - if image_path.suffix == "png": + if image_path.suffix.lower() == ".png": pil_img = Image.open(image_filename) # This chops off the alpha channel which can cause unexpected behavior, but handles minimal transparency well pil_img = pil_img.convert("RGB") @@ -53,7 +53,7 @@ def buffer_from_jpeg_file(image_filename: str) -> BufferedReader: For now, we only support JPEG files, and raise an ValueError otherwise. """ - if Path(image_filename).suffix == "jpeg": + if Path(image_filename).suffix.lower() in (".jpeg", ".jpg"): # Note this will get fooled by truncated binaries since it only reads the header. # That's okay - the server will catch it. return open(image_filename, "rb") From 28cadb0d60da5db8192dc1858812cb098781ab11 Mon Sep 17 00:00:00 2001 From: brandon Date: Tue, 3 Dec 2024 14:40:28 -0800 Subject: [PATCH 4/4] test fix --- test/integration/test_groundlight.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/integration/test_groundlight.py b/test/integration/test_groundlight.py index 904f16fb..a47b6088 100644 --- a/test/integration/test_groundlight.py +++ b/test/integration/test_groundlight.py @@ -470,11 +470,16 @@ def test_submit_image_query_bad_filename(gl: Groundlight, detector: Detector): def test_submit_image_query_bad_jpeg_file(gl: Groundlight, detector: Detector): - with pytest.raises(ValueError) as exc_info: + with pytest.raises(ApiException) as exc_info: _image_query = gl.submit_image_query( detector=detector.id, image="test/assets/blankfile.jpeg", human_review="NEVER" ) - assert "jpeg" in str(exc_info).lower() + assert "uploaded image is empty or corrupted" in exc_info.value.body.lower() + with pytest.raises(ValueError) as exc_info: + _image_query = gl.submit_image_query( + detector=detector.id, image="test/assets/blankfile.jpeeeg", human_review="NEVER" + ) + assert "we only support jpeg and png" in str(exc_info).lower() @pytest.mark.skipif(MISSING_PIL, reason="Needs pillow") # type: ignore