首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不破坏字符的情况下将图像分割成块- python

如何在不破坏字符的情况下将图像分割成块- python
EN

Stack Overflow用户
提问于 2018-08-03 07:13:35
回答 1查看 1.3K关注 0票数 1

我正在试着从课文中读取图像。如果我把图像分解成小块,我会得到更好的效果,但问题是,当我试图分割图像时,它正在切割/切割我的角色。

我正在使用的代码:

代码语言:javascript
复制
from __future__ import division

import math
import os

from PIL import Image


def long_slice(image_path, out_name, outdir, slice_size):
    """slice an image into parts slice_size tall"""
    img = Image.open(image_path)
    width, height = img.size
    upper = 0
    left = 0
    slices = int(math.ceil(height/slice_size))

    count = 1
    for slice in range(slices):
        #if we are at the end, set the lower bound to be the bottom of the image
        if count == slices:
            lower = height
        else:
            lower = int(count * slice_size)
        #set the bounding box! The important bit
        bbox = (left, upper, width, lower)
        working_slice = img.crop(bbox)
        upper += slice_size
        #save the slice
        working_slice.save(os.path.join(outdir, "slice_" + out_name + "_" + str(count)+".png"))
        count +=1

if __name__ == '__main__':
    #slice_size is the max height of the slices in pixels
    long_slice("/python_project/screenshot.png","longcat", os.getcwd(), 100)

示例图像:我要处理的图像

预期/我正在努力做的事:

我想把每一行分割成单独的图像,而不需要切割字符。

第1行:

第2行:

当前结果:图像中的字符被裁剪。

我不想根据像素切割图像,因为每个文档都有单独的间距和线宽

谢谢Jk

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 07:03:52

下面是一个解决方案,它查找图像中最亮的行(即没有文本的行),然后在这些行上拆分图像。到目前为止,我已经标记了部分,并将实际出现的情况留给您。

该算法如下:

  1. 查找每一行中每个像素的亮度之和(我只是使用红色通道)。
  2. 查找与最亮行一样亮的至少0.999和(我使用的阈值)的行。
  3. 标记那些行

下面的代码将返回这些行的列表:

代码语言:javascript
复制
def find_lightest_rows(img, threshold):
    line_luminances = [0] * img.height

    for y in range(img.height):
        for x in range(img.width):
            line_luminances[y] += img.getpixel((x, y))[0]

    line_luminances = [x for x in enumerate(line_luminances)]
    line_luminances.sort(key=lambda x: -x[1])
    lightest_row_luminance = line_luminances[0][1]
    lightest_rows = []
    for row, lum in line_luminances:
        if(lum > lightest_row_luminance * threshold):
            lightest_rows.add(row)

    return lightest_rows

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ... ]

在将这些行染成红色后,我们得到了这样的图像:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51667166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档