Last active
May 23, 2021 14:42
-
-
Save ryul99/a192310cf2cd3ce94f83b928d44a141f to your computer and use it in GitHub Desktop.
Split big image with python. You can split with split_image.py, assemble with assemble_image.py
This file contains hidden or 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
# assemble images which is splited by split_image.py | |
import cv2 | |
import glob | |
import os | |
import tqdm | |
import numpy as np | |
from itertools import islice | |
from multiprocessing import Pool | |
path = r'/path/to/common_dir/of/common_dirnames' | |
common_dirnames = [ # assemble 'common_dir_0_0', 'common_dir_0_1', 'common_dir_1_0', 'common_dir_1_1' into 'common_dir_assemble' | |
'common_dir' | |
] | |
split_img_amount = (2, 2) # Height, Width | |
def merge_image(_files): | |
assemble_dir = _files[1] | |
_files = _files[0] | |
if len(set(map(os.path.basename, _files))) != 1: | |
raise ValueError('Image names are mismatched {}'.format(set(map(os.path.basename, _files)))) | |
split_imges = [[cv2.imread(_files[i * split_img_amount[1] + j], cv2.IMREAD_UNCHANGED) for j in range(split_img_amount[1])] for i in range(split_img_amount[0])] | |
img = [] | |
for split_img in split_imges: | |
img.append(np.concatenate(split_img, axis=1)) | |
img = np.concatenate(img, axis=0) | |
cv2.imwrite(os.path.join(path, assemble_dir, os.path.basename(_files[0])), img) | |
if __name__ == '__main__': | |
num_process = 16 | |
split_files = [] | |
for common_dir in common_dirnames: | |
assemble_dir = '{}_assemble'.format(common_dir) | |
split_dir = [['{}_{}_{}'.format(common_dir, i, j) for j in range(split_img_amount[1])] for i in range(split_img_amount[0])] | |
os.mkdir(os.path.join(path, assemble_dir)) | |
temp_split_files = [sorted(glob.glob(os.path.join(path, temp_dir, '*.png'), recursive=True)) for dir_list in split_dir for temp_dir in dir_list] | |
temp_split_files = list(zip(*temp_split_files)) | |
split_files += [(zipped_file, assemble_dir) for zipped_file in temp_split_files] | |
with Pool(processes=num_process) as p: | |
r = list(tqdm.tqdm(p.imap(merge_image, split_files), total=len(split_files))) | |
This file contains hidden or 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 cv2 | |
import glob | |
import os | |
import tqdm | |
from itertools import islice | |
from multiprocessing import Pool | |
from pathlib import Path | |
path = r'path/to/file/COMMON_DIR/*.png' | |
split_img_amount = (2, 2) # Height, Width | |
split_dir = [['_{}_{}'.format(i, j) for j in range(split_img_amount[1])] for i in range(split_img_amount[0])] | |
def split_image(_file): | |
img = cv2.imread(_file, cv2.IMREAD_UNCHANGED) | |
if img.shape[0] % split_img_amount[0] != 0 or img.shape[1] % split_img_amount[1] != 0: | |
raise ValueError('Image resolution is not the multiple of number of split image') | |
stride = (img.shape[0] // split_img_amount[0], img.shape[1] // split_img_amount[1]) | |
for i in range(split_img_amount[0]): | |
for j in range(split_img_amount[1]): | |
split_img = img[i * stride[0]:(i + 1) * stride[0],j * stride[1]:(j + 1) * stride[1],:] | |
dirname = os.path.dirname(_file) | |
save_path = os.path.join(os.path.dirname(dirname), os.path.basename(dirname) + split_dir[i][j], os.path.basename(_file)) | |
Path(os.path.dirname(save_path)).mkdir(parents=True, exist_ok=True) | |
cv2.imwrite(save_path, split_img) | |
if __name__ == '__main__': | |
num_process = 16 | |
files = glob.glob(path, recursive=True) | |
# iter_files = iter(files) | |
# files = [list(islice(iter_files, e)) for e in [len(files) // num_process] * (num_process + 1)] | |
with Pool(processes=num_process) as p: | |
r = list(tqdm.tqdm(p.imap(split_image, files), total=len(files))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment