简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM