Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove imghdr #282

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/groundlight/images.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=deprecated-module
import imghdr
from io import BufferedReader, BytesIO, IOBase
from pathlib import Path
from typing import Union

from groundlight.optional_imports import Image, np
Expand Down Expand Up @@ -37,10 +37,11 @@ def bytestream_from_filename(image_filename: str, jpeg_quality: int = DEFAULT_JP
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.lower() in (".jpeg", ".jpg"):
buffer = buffer_from_jpeg_file(image_filename)
return ByteStreamWrapper(data=buffer)
if imghdr.what(image_filename) == "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")
Expand All @@ -53,7 +54,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.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")
Expand Down
9 changes: 7 additions & 2 deletions test/integration/test_groundlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading