user14587199
user14587199

Reputation:

Drawing rectangles given X and Y coords around specific brightness points- python

I began scripting code to aid me in my research in the correlation between globular clusters and galaxy types. So, my code was able to find all possible globular clusters in distance galaxy and return the X and Y coords. Now, I need to figure out how to actually be able to draw little boxes around the X and Y coords, so I can see the distribution visually. Code below:

from PIL import Image
from math import sqrt
imag = Image.open("Centaurus_A-DeNoiseAI-denoise.jpg")
imag = imag.convert ('RGB')
x=[]
y=[]
for i in range(3008):
    X,Y = i,i
    (R,G,B) = imag.getpixel((X,Y))
    brightness = sum([R,G,B])/3
    if(94<brightness<124):
        print(X,Y)
        x.append(X)
        y.append(Y)

Upvotes: 0

Views: 835

Answers (1)

ZWang
ZWang

Reputation: 892

Add this code to your already existing code

from PIL import Image, ImageDraw

#Your code from before here

with imag as im:
    delta = 5
    draw = ImageDraw.Draw(im)
    for i in range(len(x)):
        draw.rectangle([x[i-delta],y[i-delta],x[i-delta],y[i-delta]], fill=(255,0,0))

    im.save("your_image","PNG")

Adjust delta accordingly to how much padding you want on that rectangle.

Upvotes: 1

Related Questions