Skip to content

Instantly share code, notes, and snippets.

@gireeshkbogu
Last active April 8, 2020 19:03
Show Gist options
  • Save gireeshkbogu/d7f912b4a4df53b4b515d9a9fa276a73 to your computer and use it in GitHub Desktop.
Save gireeshkbogu/d7f912b4a4df53b4b515d9a9fa276a73 to your computer and use it in GitHub Desktop.
Filter images with at least one mask (sum of all pixels > 0) and copy them to a new folder
# identify images with at least one mask (sum of all pixels > 0) and copy them to a new folder
# usage: script.py mask_dir new_mask_dir
import sys
import os
from PIL import Image
import shutil
import pandas as pd
import numpy as np
#mask_dir = sys.argv[1]
#new_mask_dir = sys.argv[2]
mask_dir = "Abdomen_data/val_maskscopy/"
new_mask_dir = "Abdomen_data/val_new/"
# calcualte pixel sum of masks in each image
results1 = []
for filename in os.scandir(mask_dir):
if filename.path.endswith(".png"):
mask = Image.open(filename.path)
im_array = np.array(mask)
sum = im_array.sum()
results1.append((filename.path, sum))
results2 = []
# filter images with pixel sum >0
for x, y in results1:
if y>0:
results2.append(x)
# copies files with masks to a new directory
for x in results2:
shutil.copy(x, new_mask_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment