HyperProduct
HyperProduct

Reputation: 15

How can I add an outline to an image?

I'm fairly new to Python, and I have a python function that adds an outline to an image. However, the function currently adds the outline one pixel inside the image, instead of on the outside like it should. How can I modify this to make the outline further out by 1 pixel?

def addOutline(imageArray):
    from statistics import mean
    newAr = imageArray
    newAr.flags.writeable = True
    balance = 250
    for i0 in range(1,len(newAr)):
        for i in range(1,len(newAr[i0])):
            if mean(newAr[i0][i][:3]) > balance and mean(newAr[i0][i-1][:3]) < balance:
                newAr[i0][i-1][:3] = 40
            if mean(newAr[i0][i][:3]) < balance and mean(newAr[i0][i-1][:3]) > balance:
                newAr[i0][i][:3] = 40
            if mean(newAr[i0][i][:3]) < balance and mean(newAr[i0-1][i][:3]) > balance:
                newAr[i0][i][:3] = 40
            if mean(newAr[i0][i][:3]) < balance and mean(newAr[i0+1][i][:3]) > balance:
                newAr[i0][i][:3] = 40

    return newAr

Upvotes: 0

Views: 169

Answers (1)

mengg
mengg

Reputation: 310

I guess if you really want the outline to be outside the figure. You need to create a new image array with size (len(newAr)+1)*(len(newAr[0]+1)), where the extra pixel is to put your line. All the other pixels inside should be copied from your original image.

Upvotes: 3

Related Questions