Python Forum
Return draw.rectangle pixel coordinate data to .csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return draw.rectangle pixel coordinate data to .csv file
#1
I'm a marine science student using machine learning to automate seafloor monitoring and imagery analysis.

I already have an extensive script that's functioning well. The purpose of the script is to itterate through images and draw rectangles around a specific species within the imagery, based upon previously manually extracted pixel coordinate data in Excel spreadsheet format:

FandC = []
    for index, row in ss.iterrows():
        filename = row['filename']
        coords   = row['xyr_coords']
        x, y, r = re.findall(r'[0-9.]+',coords)
        print(f'DEBUG: filename={filename}, x={x}, y={y}, r={r}')
        FandC.append({'filename': filename, 'x':x, 'y':y, 'r':r})

master_df = pd.DataFrame(FandC)
master_df.sort_values('filename', inplace = True, axis = 0)
img1_df = master_df[master_df['filename']=='image1.jpg']

def draw_rectangle(filename, master_df):
    img_path = 'C:\\filepath\\{}'.format(filename)
    im= Image.open(img_path)
    img1_df = master_df[master_df['filename'].str.match(filename)]
    im = im.convert('RGBA')
    overlay = Image.new('RGBA', im.size)
    draw = ImageDraw.Draw(overlay)

    for index, row in img1_df.iterrows():
        for i in range(len(img1_df)):
            draw.rectangle(((float(row['x'])-float(row['r']), float(row['y'])-float(row['r'])), (float(row['x'])+float(row['r']), float(row['y'])+float(row['r']))), fill=(255,0,0,55))
            #return im.

    img = Image.alpha_composite(im, overlay)
    img = img.convert("RGB")
    img.save('C:\\filepath\\rect_{}'.format(filename))

for i, row in master_df.iterrows():
    if i == 0:
        filename_tmp = row['filename']
        draw_rectangle(filename_tmp, master_df)
    if row['filename'] == filename_tmp:
        pass
    else:
        filename_tmp = row['filename']
        draw_rectangle(filename_tmp, master_df)    
    if i == 100:      
        break
This all works as intended, and draws rectangles around multiple features within each image and exports the new annotated images to a new folder. Now however, I wish to return/extract the pixel coordinate values of each of the four corners of each rectangle drawn (as calculated in the 'draw.rectangle' line), as a function that can be intergrated into my original script. I would preferably like this in .csv format, with 5 columns aranged as (or something similar to):
'filename', top left (x/y), bottom left (x/y), top right (x/y), bottom right (x/y).

I have been fiddling with this for days and can't seem to execute what I want to achieve, so if someone could point me in the right direction that would be fantasic!

Hope this is clear. Thanks, R
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Voxel to pixel graphics voicemail 3 533 Nov-19-2023, 09:45 AM
Last Post: paul18fr
  Pixel color and action Sartre 4 1,970 Apr-13-2023, 03:26 AM
Last Post: Sartre
  Dynamic pixel display jerryf 2 675 Mar-17-2023, 07:26 PM
Last Post: jerryf
  Random coordinate generator speed improvement saidc 0 2,021 Aug-01-2021, 11:09 PM
Last Post: saidc
  Draw circle from GPS coords, and get GPX file? Winfried 0 2,145 Mar-29-2021, 07:19 PM
Last Post: Winfried
  Rotated Rectangle overlap using Shapely pyNew 0 1,676 Feb-25-2021, 04:54 AM
Last Post: pyNew
  xml file creation from an XML file template and data from an excel file naji_python 1 2,070 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Plotting Pixel Intensity Doev 0 1,703 Oct-21-2020, 10:56 PM
Last Post: Doev
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,079 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,528 May-15-2020, 12:37 PM
Last Post: scidam

Forum Jump:

User Panel Messages

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