gizp压缩包内只包含一个文件,读取时,和普通的读取文件流程相同,即
open(…) -> read(…) -> close(…)
示例
以gzip压缩包内只包含一个jpeg图像为例
In [1]: import gzip
In [2]: import cv2
In [3]: import numpy as np
In [4]: gz_file = gzip.open("C:\\img.jpeg.gz", "rb")
In [5]: img_file_buf = gz_file.read()
In [6]: type(img_file_buf), len(img_file_buf)
Out[6]: (bytes, 53773) # bytes为jpeg图像文件的所有内容,
# 53773与未压缩的jpeg图像大小相同,非gzip压缩文件大小
In [7]: img = cv2.imdecode(np.frombuffer(img_file_buf, np.uint8), 1)
In [8]: cv2.imshow("", img)
In [9]: gz_file.close()
cv2.imdecode相关参照
Python——cv2.imdecode(…)/cv2.imencode(…)从内存buffer读取/存储图像