I would like to pass the image file into OpenCV to create a NumPy array without unzipping it.
My code looks like this:
zip_file = r'D:\Folder\small_img.zip'
with zipfile.ZipFile(zip_file) as zip:
for info in zip.infolist():
path_to_image = os.path.abspath(info.filename)
zip_img = zip.open(info.filename)
pil_img = Image.open(zip_img).convert('RGB')
# cv_img = cv.imread(*)
The place where I put a *
is what I'm not sure what to pass in. I have created a few variables of class Pillow Image
(pil_img
), and ZipFileExt
(zip_img
). I understand that cv2.imread
only accepts a path. So, I have tried to pass in a path name, however I have not de-zipped the zip file, hence I also tried to create a absolute path (path_to_image
) that looks like ('D:\Folder\File1.jpg'
), and a file name (info.filename
) that looks like (File1.jpg
).
However, most of them render an error message:
TypeError: Can't convert object of type 'Image' to 'str' for 'filename'
or
TypeError: Can't convert object of type 'ZipExtFile' to 'str' for 'filename'
Or, in the case where I pass in path_to_image
or info.filename
, it returns None
.
Anyone knows what I could pass into cv2.imread
to get the NumPy array?