Python Forum
Crop Image to Bounding Box Coordinates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Crop Image to Bounding Box Coordinates
#1
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
Reply
#2
(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")
Reply
#3
That is great - thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Draw bounding boxes on live feed Jerome 0 295 Jan-20-2024, 10:50 PM
Last Post: Jerome
  Get the image's coordinates not the canvas' when navigate on an image hobbyist 9 1,827 Jul-21-2022, 03:29 PM
Last Post: deanhystad
  How to crop a video to 9:16? yschrs 3 2,570 May-14-2022, 06:48 AM
Last Post: yschrs
  How to crop eyes/nose/mouth in the face only? [OpenCV] buzzdarkyear 0 2,158 Jan-11-2022, 01:41 PM
Last Post: buzzdarkyear
  How to extract (x,y) coordinates of the boundary of a .tif image? Pythonista 23 16,354 Feb-18-2017, 09:17 AM
Last Post: merlem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020