Skip to content

Instantly share code, notes, and snippets.

@yuki-inaho
Created March 15, 2021 02:44
Show Gist options
  • Select an option

  • Save yuki-inaho/cc13aa45ecf8bb4de6a5c8d631e426dc to your computer and use it in GitHub Desktop.

Select an option

Save yuki-inaho/cc13aa45ecf8bb4de6a5c8d631e426dc to your computer and use it in GitHub Desktop.
extract_complementary_images.py
import shutil
import cv2
import numpy as np
from pathlib import Path
from tqdm import tqdm
import pdb
def get_image_pathes(input_dir_pathlib: Path):
extf = [".jpg", ".png"]
image_pathes = [path for path in input_dir_pathlib.glob("*") if path.suffix in extf]
image_path_list = [str(image_path) for image_path in image_pathes]
return image_path_list
whole_data_dir_pathlib = Path("Image_whole")
partial_data_dir_pathlib = Path("Image_partial")
output_dir_path = "./Image"
output_dir_pathlib = Path(output_dir_path)
output_dir_pathlib.mkdir(exist_ok=True)
image_list_partial = get_image_pathes(partial_data_dir_pathlib)
image_list_whole = get_image_pathes(whole_data_dir_pathlib)
image_name_list_partial = [Path(image_path).name for image_path in image_list_partial]
image_name_list_whole = [Path(image_path).name for image_path in image_list_whole]
intersect_images_name_list = np.setdiff1d(image_name_list_whole, image_name_list_partial)
for image_name in tqdm(intersect_images_name_list):
input_image_path = str(whole_data_dir_pathlib.joinpath(image_name))
image = cv2.imread(input_image_path)
output_image_name = Path(image_name).stem + ".jpg"
output_image_path = str(output_dir_pathlib.joinpath(output_image_name))
cv2.imwrite(output_image_path, image)
cv2.waitKey(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment