我正在试着从课文中读取图像。如果我把图像分解成小块,我会得到更好的效果,但问题是,当我试图分割图像时,它正在切割/切割我的角色。
我正在使用的代码:
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
发布于 2018-08-06 07:03:52
下面是一个解决方案,它查找图像中最亮的行(即没有文本的行),然后在这些行上拆分图像。到目前为止,我已经标记了部分,并将实际出现的情况留给您。
该算法如下:
下面的代码将返回这些行的列表:
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 ... ]
在将这些行染成红色后,我们得到了这样的图像:

https://stackoverflow.com/questions/51667166
复制相似问题