python 在不解压压缩包的情况下,读取zip格式的压缩文件中的图像。这里以读取bmp格式的图像为例:
少废话,直接上代码(亲测可用)
#!/usr/bin/env python3.3
# coding=utf-8
# ============================#
#
# 不解压读取.zip压缩包内的图片并显示
# ============================#
import zipfile
import cv2
import numpy as np
zipfile_path = 'C:\\Users\\YANG\\Desktop\\zip\zip.zip'
with zipfile.ZipFile(zipfile_path, mode='r') as zfile: # 只读方式打开压缩包
nWaitTime = 1
for name in zfile.namelist(): # 获取zip文档内所有文件的名称列表
if '.bmp' not in name: # 仅读取.bmp图片,过滤掉文件夹,及其他非.bmp后缀文件
continue
print(name)
with zfile.open(name, mode='r') as image_file:
content = image_file.read() # 一次性读入整张图片信息
image = np.asarray(bytearray(content), dtype='uint8')
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imshow('image', image)
key = cv2.waitKey(300)
if 27 == key: # ESC
break
elif 32 == key: # space
nWaitTime = not nWaitTime
zfile.close()