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
  Plotting Pixel Intensity Doev 0 90 Oct-21-2020, 10:56 PM
Last Post: Doev
  saving data from text file to CSV file in python having delimiter as space K11 1 243 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 328 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 375 Jun-17-2020, 06:00 PM
Last Post: buran
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 341 May-15-2020, 12:37 PM
Last Post: scidam
  Issue with creating an array of pixel data for PNG files in Google Colab The_Sarco 1 384 Apr-29-2020, 12:03 AM
Last Post: bowlofred
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 1,507 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  How to invert pixel numbers of MNIST data set squillam 1 818 Oct-16-2019, 11:18 AM
Last Post: scidam
  image grid coordinate system Bmart6969 0 344 Oct-12-2019, 05:20 AM
Last Post: Bmart6969
  Creating a Point from coordinate fields in shapefile stixmagiggins 0 390 Aug-28-2019, 06:33 AM
Last Post: stixmagiggins

Forum Jump:

User Panel Messages

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