zoukankan      html  css  js  c++  java
  • pytesseract

    import pytesseract
    from PIL import Image
    import time
    import cv2
    image = Image.open('2.png')
    image = image.convert('L') #先转灰度
    # image = image.convert('1') #再二值化,默认阀值, 不推荐,下面自定义.
    threshold = 127
    table = []
    for i in range(256):
        if i<threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table,'1')
    
    image.show()
    input()
    def noise_remove_pil(image_name, k):
        """
        8邻域降噪
        Args:
            image_name: 图片文件命名
            k: 判断阈值
    
        Returns:
    
        """
    
        def calculate_noise_count(img_obj, w, h):
            """
            计算邻域非白色的个数
            Args:
                img_obj: img obj
                w: width
                h: height
            Returns:
                count (int)
            """
            count = 0
            width, height = img_obj.size
            for _w_ in [w - 1, w, w + 1]:
                for _h_ in [h - 1, h, h + 1]:
                    if _w_ > width - 1:
                        continue
                    if _h_ > height - 1:
                        continue
                    if _w_ == w and _h_ == h:
                        continue
                    if img_obj.getpixel((_w_, _h_)) < 230:  # 这里因为是灰度图像,设置小于230为非白色
                        count += 1
            return count
    
        # img = Image.open(image_name)
        img = image_name
        # 灰度
        gray_img = img.convert('L')
    
        w, h = gray_img.size
        for _w in range(w):
            for _h in range(h):
                if _w == 0 or _h == 0:
                    gray_img.putpixel((_w, _h), 255)
                    continue
                # 计算邻域非白色的个数
                pixel = gray_img.getpixel((_w, _h))
                if pixel == 255:
                    continue
    
                if calculate_noise_count(gray_img, _w, _h) < k:
                    gray_img.putpixel((_w, _h), 255)
        return gray_img
    
    image = noise_remove_pil(image,1)
    
    image.show()
    for i in range(20):
        code = pytesseract.image_to_string(image).strip()
        print('code:',code,i)
        time.sleep(1)
    

      

  • 相关阅读:
    CSS布局简史
    行块布局(如何理解Display:None,Block,Inline,Inline-Block)
    JQuery的基础学习
    在网页中添加一个可以收藏的功能
    PHPcms需要用到
    TP框架里面当访问不存在的操作方法时让其不显示错误页面(空控制器空操作)
    验证码上传文件
    关于API
    webapp的学习
    在ThinkPHP里面进行表单验证
  • 原文地址:https://www.cnblogs.com/pythonClub/p/14903724.html
Copyright © 2011-2022 走看看