python 批量读取压缩文件(zip)中的图片(代码,已亲测)

该博客介绍了如何使用Python在不解压ZIP压缩包的情况下,读取并显示其中的BMP格式图像。通过引入zipfile和opencv库,代码实现了逐个检查ZIP文件中的文件,找到.bmp图像并进行显示。用户可以按ESC退出或空格切换图片显示速度。
摘要由CSDN通过智能技术生成

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值