lundi 31 août 2015

cv2.imdecode always returning None

I am reading binary data [sqlite3.Binary] from a sqlite BLOB in which a JPEG image is stored.

Trying to recover the image as follows:

conn = sqlite3.connect(dbname)
c = conn.cursor()
c.execute('SELECT image, height, width FROM pictures WHERE serial_no=?', (serial_no, ))
raw, height, width = c.fetchone()
raw_str = StringIO.StringIO(raw)
arr = np.asarray(bytearray(raw_str.read()), dtype=np.uint8)
img = cv2.imdecode(arr, cv2.IMREAD_COLOR)

The last step always fails and imdecode return None.

What does work, however, is a numpy.reshape:

image = arr.copy().reshape((height, width, 3))

I've tested with both OpenCV 3.0.0 and OpenCV 2.4.11 where both yield the same, unsatisfying result.

What am I missing?

Aucun commentaire:

Enregistrer un commentaire