Using my code below I have successfully been able to import an image, define a list of coordinates, and then crop the image to those coordinates. However, I have two problems with my current code:
Because the coordinates are in pairs, it is cropping the image and then cropping THAT image, rather than saving two separate crops as new images, separate from the parent image.
When I define my coordinates in the code, it works fine. But when I uncomment the first boxcrop (line 11) to get the coordinates from my csv, it doesn't work.
Eventually, I would like the code to be able to import an image, get the coordinates of the desired crops (there could be more than 2, up to 8!) from a csv file, and then save each crop as a new image, with the same filename as the original image. for example, flowers.png would become flowers_crop1, flowers_crop2, etc etc. Any and all advice is appreciated, I have looked at other posts and not seen this same issue with saving copies so I hope I am not re-asking a question.
from PIL import Image
import numpy as np
import pandas as pd
#Open image
im = Image.open(r'C:/Users/Testing/Capture.png')
#Open excel file
df = pd.read_csv(r'C:/Users/Testing/crops.csv', header=0)
#Get coordinates of box
#boxcrop = df.values.T[2].tolist()
boxcrop = ['(212,233,226,247)','(196,217,210,231)']
for i in boxcrop:
left, upper, right, lower = np.array([i.replace('(', '').replace(')','').split(',')], dtype=int).T
dims = np.concatenate([left, upper, right, lower])
im_crop = im.crop((dims))
im_crop.save(r'C:\Users\Testing\crops\cropped.png', quality=95)