Python Forum

Full Version: Crop Image to Bounding Box Coordinates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

Still very much a beginner to the world of Python and would be grateful for any help to solve an issue!

I have run a YOLO Object Detector algorithm, and have a csv file containing an image name e.g. aoi_yymmdd.png and the object detector bounding box coordinates (xmin, ymin, xmax, ymax). I would like to read this file then save the output file as "bbox_" + "orginal_image_filename". I cannot get the code right so it iterates through all image files in increments, then save with a unique output filename...

I have been attempting to use a list to create an output file name:
for i in list(image_files):
              print("bbox_"+i)

out_image = "bbox_" + str(i)
I have the output directory open when I run the code, and see all cropped images, but as the filename is the same each time, the outputs just get replaced.
How can I create a unique output filename?
for row in csv_f:
        im = Image.open(row[0])
        cropbbox = (row[1], row[2], row[3], row[4])
        cropbbox = map(int, cropbbox)
        im = im.crop((cropbbox))
        im.save(output + out_image + ".png")
Thanks in advance Smile
(Jul-17-2020, 03:32 PM)sallyjc81 Wrote: [ -> ]How can I create a unique output filename?
You can append the bbox specification to each filename, e.g.

im.save(f"{output}-subimage-{row[1]}_{row[2]}_{row[3]}_{row[4]}.png")
That is great - thank you!