Python Forum
Need help creating complex loop around existing script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help creating complex loop around existing script
#1
Hi all,

I have a script used to draw rectangles over multiple features within an image, based upon the pre-extracted x/y/r pixel coordinates of said features. This script is as follows:

import os
import numpy as np
import pandas as pd
import PIL
from PIL import Image
from PIL import ImageDraw
import glob
import re                       # import RegEx module

xeno_data = 'C:\file_path\data.xlsx'
ss = pd.read_excel(xeno_data)
These three pixel coord values (x/y/r) are all contained within the same column in the Excel spreadsheet, hence the following step to seperate them into seperate numbers:

# Extract filenames and coordinates from Excel spreadsheet (data.xlsx):
FandC = []
for index, row in ss.iterrows():
   filename = row['filename']
   coords   = row['xyr_coords']
       # Use RegEx to find anything that looks like a group of digits, possibly seperated by decimal point:
   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})
Creating new dataframes for the values of interest - 'filename', 'x', 'y', and 'r':

fandc=pd.DataFrame(FandC)
    #creates a dataframe for "filename", "x", "y", and "r".

fandc['filename'] [fandc['filename']=='Image1.jpg']
    # shows "fandc['filename']" where the "filename" is equal to (==) the string "Image1.jpg".

fandc_f = fandc[fandc['filename']=='Image1.jpg']
    # create new df called "fandc_f" that only includes "fandc['filename']" where "filename" == "Image1.jpg".

for index , row in fandc_f.iterrows():
    row
    break
Draw a transparent rectangle:

im = im.convert('RGBA')
overlay = Image.new('RGBA', im.size)
draw = ImageDraw.Draw(overlay)

for index, row in fandc_f.iterrows():
    for i in range(len(fandc_f)):
        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))

# Remove alpha for saving in jpg format:        
img = Image.alpha_composite(im, overlay)
img = img.convert("RGB")
This code is working perfectly... for one image. However, now I need to wrap the whole process in with a big loop on the outside that automatically goes through each unique filenames in a pandas df.

For each one I need it to:
  • load the image,
  • filter the df for respective filenames,
  • apply my rectangle drawing loop,
  • save the image,
  • and then loop on to next image automatically (as I have thousands of images to process, and cannot do it manually).

If any clarification is needed ask away! Big Grin

Thanks, Rhod
Reply


Messages In This Thread
Need help creating complex loop around existing script - by CephloRhod - Apr-11-2020, 09:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 994 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  Python beginner that needs an expression added to existing script markham 1 743 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 963 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,554 Jul-27-2022, 08:50 PM
Last Post: rob101
  how to loop a script? ZYSIA 1 2,050 Jul-22-2021, 06:46 AM
Last Post: Gribouillis
  creating a loop yk303 2 1,893 Feb-08-2021, 08:41 PM
Last Post: nilamo
  Help with Creating a Script for Automating Reports SunWers 1 1,959 Dec-29-2020, 10:21 PM
Last Post: jjc385
  Creating a variables inside FOR loop zazas321 5 4,167 Sep-16-2020, 04:42 PM
Last Post: Naheed
  Creating Complex Color Spectrum Plot From Data JoeDainton123 2 2,172 Sep-15-2020, 08:09 AM
Last Post: DPaul
  Need help creating a simple script Nonameface 12 4,710 Jul-14-2020, 02:10 PM
Last Post: BitPythoner

Forum Jump:

User Panel Messages

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