Skip to content

Commit

Permalink
remove imghdr
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-groundlight committed Dec 3, 2024
1 parent 5e9da70 commit 0bbdb6a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/groundlight/images.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pylint: disable=deprecated-module
import imghdr
from pathlib import Path
from io import BufferedReader, BytesIO, IOBase
from typing import Union

Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 0bbdb6a

Please sign in to comment.