Skip to content

Commit 512f75b

Browse files
brandon-wadaAuto-format Bot
andauthored
Remove imghdr (#282)
Give a 21 print line salute to imghdr which is slated for removal from the standard library --------- Co-authored-by: Auto-format Bot <autoformatbot@groundlight.ai>
1 parent 5e9da70 commit 512f75b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/groundlight/images.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pylint: disable=deprecated-module
2-
import imghdr
32
from io import BufferedReader, BytesIO, IOBase
3+
from pathlib import Path
44
from typing import Union
55

66
from groundlight.optional_imports import Image, np
@@ -37,10 +37,11 @@ def bytestream_from_filename(image_filename: str, jpeg_quality: int = DEFAULT_JP
3737
Only supports JPEG and PNG files for now.
3838
For PNG files, we convert to RGB format used in JPEGs.
3939
"""
40-
if imghdr.what(image_filename) == "jpeg":
40+
image_path = Path(image_filename)
41+
if image_path.suffix.lower() in (".jpeg", ".jpg"):
4142
buffer = buffer_from_jpeg_file(image_filename)
4243
return ByteStreamWrapper(data=buffer)
43-
if imghdr.what(image_filename) == "png":
44+
if image_path.suffix.lower() == ".png":
4445
pil_img = Image.open(image_filename)
4546
# This chops off the alpha channel which can cause unexpected behavior, but handles minimal transparency well
4647
pil_img = pil_img.convert("RGB")
@@ -53,7 +54,7 @@ def buffer_from_jpeg_file(image_filename: str) -> BufferedReader:
5354
5455
For now, we only support JPEG files, and raise an ValueError otherwise.
5556
"""
56-
if imghdr.what(image_filename) == "jpeg":
57+
if Path(image_filename).suffix.lower() in (".jpeg", ".jpg"):
5758
# Note this will get fooled by truncated binaries since it only reads the header.
5859
# That's okay - the server will catch it.
5960
return open(image_filename, "rb")

test/integration/test_groundlight.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,16 @@ def test_submit_image_query_bad_filename(gl: Groundlight, detector: Detector):
470470

471471

472472
def test_submit_image_query_bad_jpeg_file(gl: Groundlight, detector: Detector):
473-
with pytest.raises(ValueError) as exc_info:
473+
with pytest.raises(ApiException) as exc_info:
474474
_image_query = gl.submit_image_query(
475475
detector=detector.id, image="test/assets/blankfile.jpeg", human_review="NEVER"
476476
)
477-
assert "jpeg" in str(exc_info).lower()
477+
assert "uploaded image is empty or corrupted" in exc_info.value.body.lower()
478+
with pytest.raises(ValueError) as exc_info:
479+
_image_query = gl.submit_image_query(
480+
detector=detector.id, image="test/assets/blankfile.jpeeeg", human_review="NEVER"
481+
)
482+
assert "we only support jpeg and png" in str(exc_info).lower()
478483

479484

480485
@pytest.mark.skipif(MISSING_PIL, reason="Needs pillow") # type: ignore

0 commit comments

Comments
 (0)