如何为图像添加轮廓?

时间:2016-09-25 19:50:12

标签: python image python-3.x pillow

我对Python很新,我有一个python函数,可以为图像添加轮廓。但是,该功能目前在图像内部添加了一个像素,而不是像应该的那样在外部添加。如何修改它以使轮廓进一步缩小1个像素?

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

1 个答案:

答案 0 :(得分:3)

我想如果你真的希望轮廓超出这个数字。您需要创建一个大小为(len(newAr)+1)*(len(newAr[0]+1))的新图像数组,其中额外的像素是放置您的线。应该从原始图像中复制内部的所有其他像素。