Builds a single .npz file from a folder of .png samples.
(sample_dir, num=50_000)
| 32 | |
| 33 | |
| 34 | def create_npz_from_sample_folder(sample_dir, num=50_000): |
| 35 | """ |
| 36 | Builds a single .npz file from a folder of .png samples. |
| 37 | """ |
| 38 | samples = [] |
| 39 | for i in tqdm(range(num), desc="Building .npz file from samples"): |
| 40 | sample_pil = Image.open(f"{sample_dir}/{i:06d}.png") |
| 41 | sample_np = np.asarray(sample_pil).astype(np.uint8) |
| 42 | samples.append(sample_np) |
| 43 | samples = np.stack(samples) |
| 44 | assert samples.shape == (num, samples.shape[1], samples.shape[2], 3) |
| 45 | npz_path = f"{sample_dir}.npz" |
| 46 | np.savez(npz_path, arr_0=samples) |
| 47 | print(f"Saved .npz file to {npz_path} [shape={samples.shape}].") |
| 48 | return npz_path |
| 49 | |
| 50 | |
| 51 | def main(args): |