python 处理图片像素点(1)

在做爬虫的时候有时需要识别验证码,但是验证码一般都有干扰物,这时需要对验证码进行预处理,效果如下:

这里写图片描述 
这里写图片描述

from PIL import Image
import itertools
img = Image.open('C:/img.jpg').convert('L') #打开图片,convert图像类型有L,RGBA

# 转化为黑白图
def blackWrite(img):
    blackXY = []

    # 遍历像素点
    for x in range(img.size[0]):
        for y in range(img.size[1]):
            print img.getpixel((x,y))
            if img.getpixel((x,y))<128:
                img.putpixel((x,y),0) # 置为黑点
                blackXY.append((x,y))
            else:
                img.putpixel((x,y),255) # 置为白点
    return blackXY


# 去除干扰点
def clrImg(img,pointArr):
    # 获取周围黑点的个数
    def getN(p):
        count = 0
        x = [p[0]-1,p[0],p[0]+1]
        y = [p[1]-1,p[1],p[1]+1]
        for i in itertools.product(x,y):  # 笛卡尔积
            try:
                if img.getpixel(i) == 0:
                    count +=1
            except:
                print 'out of'
                continue
        print count
        return count

    for p in pointArr:
        if getN(p)<5:  #  周围黑点个数 <5 的黑点认为是干扰点,置为白点
            img.putpixel(p,255)

pointArr =  blackWrite(img)
clrImg(img,pointArr)
img.save("C:/img_1.jpg")

猜你喜欢

转载自blog.csdn.net/qq_38839677/article/details/81912902
今日推荐