zoukankan      html  css  js  c++  java
  • 验证码识别之图像切割算法(二)

    备注(易拍全球)

    切割前:       切割后:                                 

    切割前:     切割后:                    

    好了,上代码:

    #-*-coding:utf-8-*-
    
    
    from PIL import Image
    
    
    
    def smartSliceImg(img, outDir, count=4, p_w=3):
        '''
        :param img:
        :param outDir:
        :param count: 图片中有多少个图片
        :param p_w: 对切割地方多少像素内进行判断
        :return:
        '''
        w, h = img.size
        pixdata = img.load()
        eachWidth = int(w / count)
        beforeX = 0
        for i in range(count):
    
            allBCount = []
            nextXOri = (i + 1) * eachWidth
    
            for x in range(nextXOri - p_w, nextXOri + p_w):
                if x >= w:
                    x = w - 1
                if x < 0:
                    x = 0
                b_count = 0
                for y in range(h):
                    if pixdata[x, y] == 0:
                        b_count += 1
                allBCount.append({'x_pos': x, 'count': b_count})
            sort = sorted(allBCount, key=lambda e: e.get('count'))
    
            nextX = sort[0]['x_pos']
            box = (beforeX, 0, nextX, h)
            img.crop(box).save(outDir + str(i) + ".png")
            beforeX = nextX
    
    
    img = Image.open('2.jpg')
    outDir = 'cd/'
    smartSliceImg(img, outDir, count=4, p_w=3)
  • 相关阅读:
    Python 学习目录
    Django目录
    SQLAlchemy
    Flask之Sqlalchemy
    Websocket
    Mongodb
    虚拟环境
    Github
    LINUX
    内存管理和垃圾回收机制
  • 原文地址:https://www.cnblogs.com/xuchunlin/p/9290226.html
Copyright © 2011-2022 走看看