Last active
February 5, 2020 12:09
-
-
Save ihoromi4/3b94ea234a4e5da0b9f9071e4e73028d to your computer and use it in GitHub Desktop.
convert numpy images to zip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import zipfile | |
import numpy as np | |
import cv2 | |
def images_to_zip(images: list, out_filename: str): | |
if os.path.exists(out_filename): | |
print('File', out_filename, 'already exists. Abort.') | |
return | |
with zipfile.ZipFile(out_filename, 'w') as f_zip: | |
for i, image in enumerate(images): | |
assert isinstance(image, np.ndarray) | |
name = str(i) | |
image = cv2.imencode('.png', image)[1] | |
f_zip.writestr(name + '.png', image) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import zipfile | |
import numpy as np | |
import cv2 | |
def load_images_from_zip(filename: str, cachemem: bool = False) -> list: | |
if cachemem: | |
with open(filename, 'rb') as f: | |
filename = io.BytesIO(f.read()) | |
images = [] | |
with zipfile.ZipFile(filename, 'r') as f_zip: | |
names = self.file.namelist() | |
for name in names: | |
buffer = np.frombuffer(f_zip.read(name), dtype='uint8') | |
image = cv2.imdecode(buffer, cv2.IMREAD_GRAYSCALE) | |
images.append(image) | |
return images |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment