阿里汇川验证码图像处理

2019-04-07  本文已影响0人  dairoot

验证码获取地址

原始验证码

去除图片干扰线

# coding: utf-8
from PIL import Image
import numpy as np

def img_proce(img_path):
    '''图片处理 去除干扰线'''
    img = Image.open(img_path)
    img2 = Image.new("RGB", img.size, 255)
    for x in range(img.size[1]):
        for y in range(img.size[0]):
            pix = img.getpixel((y, x))
            img2.putpixel((y, x), pix)
            if pix[0] == pix[1] == pix[2]:
                img2.putpixel((y, x), (255,255,255))
    return img2

if __name__ == '__main__':
    img2 = img_proce("auth.png")
    img2.save("lines.png")
去除干扰线

修复图像

def img_proce(img_path):
    '''图片处理 去除干扰线'''
    img = Image.open(img_path)
    img2 = Image.new("RGB", img.size, 255)
    for x in range(img.size[1]):
        for y in range(img.size[0]):
            pix = img.getpixel((y, x))
            img2.putpixel((y, x), pix)
            if pix[0] == pix[1] == pix[2]:
                img2.putpixel((y, x), (255,255,255))

                # 修复图像
                if pix in [(0, 0, 0)]:
                    if x > 0 and x < img.size[1] - 1:
                        down_pix = img.getpixel((y, x+1))
                        up_pix = img.getpixel((y, x-1))
                        new_pix = tuple([sum(x) for x in zip(up_pix, down_pix)])
                        if new_pix not in [(0, 0, 0)]:
                            img2.putpixel((y, x), new_pix)
    return img2

if __name__ == '__main__':
    img2 = img_proce("auth.png")
    img2.save("1.png")
修复后图片

图片转为灰度并且去除噪点

def get_weight(img, y, x, threshold):
   ''' 获取该点周围的有效值数量 '''
   point_pix = {}
   if x > 0:
       # up
       point_pix['up'] = img.getpixel((y, x-1))

   if  x < img.size[1] - 1:
       # down
       point_pix['down'] = img.getpixel((y, x+1))

   if y > 0:
       # left
       point_pix['left'] = img.getpixel((y-1, x))

   if y< img.size[0] - 1:
       #right
       point_pix['right'] = img.getpixel((y+1, x))

   if y > 0  and x >0:
       # up left
       point_pix['up_left'] = img.getpixel((y-1, x-1))

   if  y< img.size[0] - 1 and x >0:
       # up right
       point_pix['up_right'] = img.getpixel((y+1, x-1))

   if y > 0  and x < img.size[1] - 1:
       # down left
       point_pix['down_left'] = img.getpixel((y-1, x+1))

   if  y< img.size[0] - 1 and x < img.size[1] - 1:
       # down right
       point_pix['down_right'] = img.getpixel((y+1, x+1))

   return sum([x < threshold for x in point_pix.values()])


def denoise_img(img2):
   ''' 噪点处理 '''
   img = img2.convert("L")
   img3 = Image.new("L", img.size, 255)
   for x in range(img.size[1]):
       for y in range(img.size[0]):
           pix = img.getpixel((y, x))
           img3.putpixel((y, x), 255)
           threshold = 200
           if pix < threshold:
               w = get_weight(img, y, x, threshold)
               if w > 4:
                   img3.putpixel((y, x), 0)
   return img3

if __name__ == '__main__':
   img2 = img_proce("auth.png")
   img2.save("1.png")
   img3 = denoise_img(img2)
   img3.save("3.png")
灰度降噪处理
def split_img(img):
    # 图片分割
    width = img.size[0] // 4
    for x in range(0, img.size[0], width):
        img1 = img.crop([x,0,x+width,img.size[1]])
        img1.save("split_img%s.png" % x)


if __name__ == '__main__':
    img2 = img_proce("auth.png")
    img2.save("1.png")
    img3 = denoise_img(img2)
    img3.save("3.png")

    split_img(img3)
分割处理
上一篇下一篇

猜你喜欢

热点阅读