Last active
November 14, 2018 04:00
-
-
Save serihiro/64357e98bea2e061954eb275ff84a4d3 to your computer and use it in GitHub Desktop.
Find gray scale image and generate a list of them
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
from PIL import Image | |
import numpy as np | |
import argparse | |
import glob | |
import sys | |
import concurrent.futures | |
def is_gray_scale(path): | |
try: | |
if len(np.asarray(Image.open(path)).shape) == 2: | |
return True | |
else: | |
return False | |
except Exception as err: | |
print(f'error at {path}, {err}') | |
True | |
def find_gray_scale_image(path): | |
if is_gray_scale(path): | |
return path | |
else: | |
return None | |
def main(): | |
parser = argparse.ArgumentParser(description = "Remove only gray scale image file script") | |
parser.add_argument("--root", required = True) | |
parser.add_argument("--out", required = True) | |
parser.add_argument("--image_type", default="JPEG") | |
parser.add_argument("--concurrency", default=1) | |
args = parser.parse_args() | |
root_path = f'{args.root}/**/*.{args.image_type}' | |
all_path = glob.glob(root_path, recursive=True) | |
print(f'target images count: {len(all_path)}') | |
delete_target = [] | |
with concurrent.futures.ProcessPoolExecutor(max_workers=args.concurrency) as executor: | |
raw_result = list(executor.map(find_gray_scale_image, all_path)) | |
delete_target = [x for x in raw_result if x] | |
with open(args.out, "w") as f: | |
f.write("\n".join(delete_target)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment