Skip to content

Instantly share code, notes, and snippets.

@robgon-art
Last active July 30, 2020 18:09
Show Gist options
  • Save robgon-art/f43c9f6cf9efa0aa383c4d67f3316eaf to your computer and use it in GitHub Desktop.
Save robgon-art/f43c9f6cf9efa0aa383c4d67f3316eaf to your computer and use it in GitHub Desktop.
Automatically remove frames from pictures of paintings.
# set the file paths
from_path = 'art/wikiart/'
to_path = 'art/cropped/'
# set up some paramters
thresh1 = 15000
thresh2 = 30
pad = 30
# loop through each of the files
for file in listdir(from_path):
img = Image.open(from_path + '/' + file)
w_pad = img.width//pad
h_pad = img.height//pad
# get the median RGB value
r_global_med = np.median(np_img[h_pad:-h_pad,w_pad:-w_pad,0:1])
g_global_med = np.median(np_img[h_pad:-h_pad,w_pad:-w_pad,1:2])
b_global_med = np.median(np_img[h_pad:-h_pad,w_pad:-w_pad,2:3])
# find the inner edges of the frames
left = find_left()
top = find_top()
right = find_right(img.width)
bottom = find_bottom(img.height)
# crop the image
cropped_img = img.crop((left, top, right, bottom))
# save it
cropped_img.save(to_path + file[:-4] + "_cropped.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment