Skip to content

Instantly share code, notes, and snippets.

@ryul99
Created April 9, 2020 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryul99/cc065b8b950bc0b82d94f32e31a329dd to your computer and use it in GitHub Desktop.
Save ryul99/cc065b8b950bc0b82d94f32e31a329dd to your computer and use it in GitHub Desktop.
import cv2
import glob
import os
import tqdm
from itertools import islice
from multiprocessing import Pool
from pathlib import Path
path = r'./**/*.png'
resize_factor = (0.5, 0.5) # Width, Height
def resize_image(_file):
img = cv2.imread(_file, cv2.IMREAD_UNCHANGED)
resized_img = cv2.resize(img, None, fx=resize_factor[0], fy=resize_factor[1])
save_path = os.path.join('../resized_image', _file)
Path(os.path.dirname(save_path)).mkdir(parents=True, exist_ok=True)
cv2.imwrite(save_path, resized_img)
if __name__ == '__main__':
num_process = 16
files = glob.glob(path, recursive=True)
len_total = len(files)
# 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(resize_image, files), total=len_total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment