码迷,mamicode.com
首页 > 编程语言 > 详细

python 验证码获取后处理降噪、灰度、保存

时间:2019-12-27 13:30:33      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:ima   pixel   span   process   image   def   RoCE   data   res   

def convert_image(self):
        image_obj = Image.open(self.captcha_path)# 获取验证码
        img = image_obj.convert("L")  # 转灰度
        pixdata = img.load()
        w, h = img.size
        threshold = 160
        # 遍历所有像素,大于阈值的为黑色
        for y in range(h):
            for x in range(w):
                if pixdata[x, y] < threshold:
                    pixdata[x, y] = 0
                else:
                    pixdata[x, y] = 255
        return img

    def process_image(self):
        images = self.convert_image()
        data = images.getdata()
        w, h = images.size
        black_point = 0
        for x in range(1, w - 1):
            for y in range(1, h - 1):
                mid_pixel = data[w * y + x]  # 中央像素点像素值
                if mid_pixel < 50:  # 找出上下左右四个方向像素点像素值
                    top_pixel = data[w * (y - 1) + x]
                    left_pixel = data[w * y + (x - 1)]
                    down_pixel = data[w * (y + 1) + x]
                    right_pixel = data[w * y + (x + 1)]
                    # 判断上下左右的黑色像素点总个数
                    if top_pixel < 10:
                        black_point += 1
                    if left_pixel < 10:
                        black_point += 1
                    if down_pixel < 10:
                        black_point += 1
                    if right_pixel < 10:
                        black_point += 1
                    if black_point < 1:
                        images.putpixel((x, y), 255)
                    black_point = 0

            images.save(self.process_captcha_path)
        #return images

  

python 验证码获取后处理降噪、灰度、保存

标签:ima   pixel   span   process   image   def   RoCE   data   res   

原文地址:https://www.cnblogs.com/winstonsias/p/12106671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!