from PIL import Image
def makeSketch(img, threshold):
w,h = img.size
#图像转换为灰度模式
img = img.convert('L')
#获取灰度矩阵
pix = img.load()
for x in range(w-1):
for y in range(h-1):
if abs(pix[x,y] - pix[x+1,y+1]) >= threshold:
pix[x,y] = 0
else:
pix[x,y]=255
return img
img = Image.open("E:/image/1.png")
newImg = makeSketch(img, 20)
newImg.show()