python 垂直投影分割

def vertical(img, threashold, outDir):
    '''
    :param img:
    :param threashold: 阀值
    :param outDir: 保存位置
    :return:
    '''
    w, h = img.size
    pixdata = img.load()

    x_array = []
    startX = 0
    endX = 0
    for x in range(w):
        b_count = 0
        for y in range(h):
            if pixdata[x, y] <= threashold:
                b_count += 1
        if b_count > 0:
            if startX == 0:
               startX = x
        elif b_count == 0:
            if startX != 0:
               endX = x
               x_array.append({'startX':startX, 'endX':endX})
               startX = 0
               endX = 0
    for i, item in enumerate(x_array):
        box = (item['startX'], 0, item['endX'], h)
        img.crop(box).save(outDir + str(i) + ".png")
 

img = Image.open('out/51.png')
vertical(img, 30, 'out1/')

           

 
 

http://www.waitingfy.com/archives/3807

 

你可能感兴趣的:(python)