Reputation: 2003
Why can't this specific test.png
image be processed by OpenCV?
Note: I posted a workaround, but I am still interested in what the actual cause of the error is.
import cv2
cv2.imread('test.png')
results in
libpng error: Read Error
, while other .png
s get processed without error.
cv2.version.opencv_version == '4.5.5.62'
installed via pipenv install opencv-python
on python 3.8
Upvotes: 0
Views: 2536
Reputation: 2003
I noticed that version of libpng
is 15.13.0
according to the file name of
lib/python3.8/site-packages/opencv_python.libs/libpng15-ce838cd1.so.15.13.0
Apparently, it's a bug in an older version of libpng
, as PIL/pillow
using libpng16
(Pillow.libs/libpng16-213e245f.so.16.37.0
) works just fine.
# instead of:
# image = cv2.imread(img_name)
# use:
image = Image.open(img_name)
# Convert Image to numpy array
# It's not the most efficient way, but it works. *(link¹)
image = np.asarray(image)
# Remove alpha channel if existent
if len(image.shape) == 3 and image.shape[2] == 4:
image = image[:, :, : 3]
# Restore RGB colors
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
References:
Performance: https://uploadcare.com/blog/fast-import-of-pillow-images-to-numpy-opencv-arrays/
Upvotes: 2