Skip to content

Instantly share code, notes, and snippets.

@ryul99
Created April 9, 2020 05:59
Show Gist options
  • Select an option

  • Save ryul99/cc065b8b950bc0b82d94f32e31a329dd to your computer and use it in GitHub Desktop.

Select an option

Save ryul99/cc065b8b950bc0b82d94f32e31a329dd to your computer and use it in GitHub Desktop.

Revisions

  1. ryul99 created this gist Apr 9, 2020.
    26 changes: 26 additions & 0 deletions resize_image.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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))