Skip to content

Commit b5b0635

Browse files
committed
fixes opencv issue with avif/heic
1 parent 9ba184b commit b5b0635

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ matplotlib
66
numpy>=1.18.5
77
opencv-python-headless==4.10.0.84
88
Pillow>=7.1.2
9-
pillow-avif-plugin>=1.5.0
109
pillow-heif>=0.18.0
1110
python-dateutil
1211
python-dotenv

roboflow/util/image_utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
import yaml
88
from PIL import Image
99

10+
try:
11+
import pillow_heif
12+
13+
pillow_heif.register_heif_opener(thumbnails=False) # Register for HEIF/HEIC
14+
pillow_heif.register_avif_opener() # Register for AVIF
15+
except ImportError:
16+
pass
17+
1018

1119
def check_image_path(image_path):
1220
"""
@@ -74,9 +82,18 @@ def validate_image_path(image_path):
7482
def file2jpeg(image_path):
7583
import cv2
7684

85+
# OpenCV will handle standard formats efficiently
7786
img = cv2.imread(image_path)
78-
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
79-
pilImage = Image.fromarray(image)
87+
if img is not None:
88+
# Convert BGR to RGB for PIL
89+
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
90+
pilImage = Image.fromarray(image)
91+
else:
92+
# If OpenCV fails, the format might be HEIC/AVIF which are handled by PIL
93+
pilImage = Image.open(image_path)
94+
if pilImage.mode != "RGB":
95+
pilImage = pilImage.convert("RGB")
96+
8097
buffered = io.BytesIO()
8198
pilImage.save(buffered, quality=100, format="JPEG")
8299
return buffered.getvalue()

0 commit comments

Comments
 (0)