Skip to content

Instantly share code, notes, and snippets.

@serihiro
Last active November 14, 2018 04:00
Show Gist options
  • Save serihiro/64357e98bea2e061954eb275ff84a4d3 to your computer and use it in GitHub Desktop.
Save serihiro/64357e98bea2e061954eb275ff84a4d3 to your computer and use it in GitHub Desktop.
Find gray scale image and generate a list of them
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