我能够从zip成功加载图像:
with zipfile.ZipFile('test.zip', 'r') as zfile:
data = zfile.read('test.jpg')
# how to open this using imread or imdecode?
问题是:如何在不首先保存图像的情况下使用imread或imdecode打开此文件以在opencv中进行进一步处理?
更新:
在这里,我得到预期的错误。我需要将“数据”转换为opencv可以使用的类型。
data = zfile.read('test.jpg')
buf = StringIO.StringIO(data)
im = cv2.imdecode(buf, cv2.IMREAD_GRAYSCALE)
# results in error: TypeError: buf is not a numpy array, neither a scalar
a = np.asarray(buf)
cv2.imdecode(a, cv2.IMREAD_GRAYSCALE)
# results in error: TypeError: buf data type = 17 is not supported
用于numpy.frombuffer()
从字符串创建uint8数组:
import zipfile
import cv2
import numpy as np
with zipfile.ZipFile('test.zip', 'r') as zfile:
data = zfile.read('test.jpg')
img = cv2.imdecode(np.frombuffer(data, np.uint8), 1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句