cv2.imread does not read jpg files

Something is off in your build of cv2. Rebuild it from source, or get it from the package manager.

As a workaround, load jpeg files with matplotlib instead:

>>> import cv2
>>> import matplotlib.pyplot as plt
>>> a1 = cv2.imread('pic1.jpg')
>>> a1.shape
(286, 176, 3)
>>> a2 = plt.imread('pic1.jpg')
>>> a2.shape
(286, 176, 3)

Note that opencv and matplotlib read the colour channels differently by default (one is RGB and one is BGR). So if you rely on the colours at all, you had better swap the first and third channels, like this:

>>> a2 = a2[..., ::-1]  # RGB --> BGR
>>> (a2 == a1).all()
True

Other than that, cv2.imread and plt.imread should return the same results for jpeg files. They both load into 3-channel uint8 numpy arrays.

Leave a Comment