Add Line.baseline property.

This commit is contained in:
Andrey Golovizin 2014-08-13 15:30:04 +02:00
parent b756ea484b
commit 7ce6fa42c1
2 changed files with 14 additions and 1 deletions

View file

@ -68,7 +68,8 @@ class PageScene(QGraphicsScene):
for letter in word:
if not letter.image.isspace:
self.addRect(letter.x1, letter.y1, letter.width, letter.height, letterPen, letterBrush)
self.addRect(line.x1, line.y1, line.width, line.height, Qt.red)
self.addLine(line.x1, line.y1 + line.baseline, line.x2, line.y1 + line.baseline, linePen)
# self.addRect(line.x1, line.y1, line.width, line.height, Qt.red)
def addPage(self, page):
qimage = ndimage2qimage(page.image.data)

View file

@ -14,7 +14,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
from scipy import ndimage
from scipy.ndimage import filters
from .utils import cached_property, collect_iterable
from .image import Image
@ -85,6 +87,16 @@ class Line(PageObject):
for word in self.words:
yield from word.letters
@cached_property
def baseline(self):
"""Detect baseline height, relative to the top."""
bitmap = self.image.bitmap.astype(np.float)
histogram = bitmap.sum(axis=1)
gradient = filters.convolve1d(histogram, [1, -1], axis=0)
# top = gradient.argmax()
bottom = gradient.argmin()
return bottom
class Word(PageObject):
def __iter__(self):