Skip to content

Instantly share code, notes, and snippets.

@noamsgl
Last active July 31, 2022 14:51
Show Gist options
  • Save noamsgl/7acebd5ee480de27fb3825d450478627 to your computer and use it in GitHub Desktop.
Save noamsgl/7acebd5ee480de27fb3825d450478627 to your computer and use it in GitHub Desktop.
A script to crop and wrangle labels
import os
import pandas as pd
from PIL import Image
def crop_cell(row):
"""
crop_cell(row)
given a pd.Series row of the dataframe, load row['filename'] with PIL,
crop it to the box row['xmin'], row['xmax'], row['ymin'], row['ymax']
save the cropped image,
return cropped filename
"""
input_dir = 'BCCD\JPEGImages'
output_dir = 'BCCD\cropped'
# open image
im = Image.open(f"{input_dir}\{row['filename']}")
# size of the image in pixels
width, height = im.size
# setting the points for cropped image
left = row['xmin']
bottom = row['ymax']
right = row['xmax']
top = row['ymin']
# cropped image
im1 = im.crop((left, top, right, bottom))
cropped_fname = f"BloodImage_{row['image_id']:03d}_{row['cell_id']:02d}.jpg"
# shows the image in image viewer
# im1.show()
# save image
try:
im1.save(f"{output_dir}\{cropped_fname}")
except:
return 'error while saving image'
return cropped_fname
if __name__ == "__main__":
# load labels csv into Pandas DataFrame
filepath = "BCCD\dataset2-master\labels.csv"
df = pd.read_csv(filepath)
# iterate through cells, crop each cell, and save cropped cell to file
dataset_df['cell_filename'] = dataset_df.apply(crop_cell, axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment