pixelocr/pixelocr/gui/pageview.py
Andrey Golovizin 4bb1513cd9 Fix a typo.
2014-08-25 13:52:24 +02:00

99 lines
3.1 KiB
Python

# Copyright (C) 2014 Andrey Golovizin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt4.QtCore import (
signal,
slot,
)
from PyQt4.QtGui import (
QBrush,
QColor,
QFrame,
QGraphicsDropShadowEffect,
QGraphicsItem,
QGraphicsScene,
QGraphicsView,
QImage,
QPalette,
QPen,
QPixmap,
)
import numpy as np
from scipy.ndimage import imread
class PageScene(QGraphicsScene):
page = None
pageItem = None
glyphPen = QColor(0, 0, 0, 80)
glyphBrush = QColor(255, 255, 0, 80)
spacePen = QColor(0, 0, 0, 80)
spaceBrush = QColor(0, 0, 255, 20)
linePen = QColor(255, 50, 50, 100)
pageItemChanged = signal([QGraphicsItem])
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.highlightedGlyphs = {}
self.setBackgroundBrush(self.palette().window())
def setPage(self, page):
self.highlightedGlyphs.clear()
for item in self.items():
self.removeItem(item)
self.page = page
self.pageItem = self.addPage(page)
shadow = QGraphicsDropShadowEffect()
shadow.setBlurRadius(20)
shadow.setOffset(2, 2)
self.pageItem.setGraphicsEffect(shadow)
self.sceneRectChanged.emit(self.sceneRect())
self.pageItemChanged.emit(self.pageItem)
def highlightAll(self):
for line in page:
for glyph in line:
if not glyph.image.isspace:
self.addRect(glyph.x - 1, glyph.y - 1, glyph.width + 1, glyph.height + 1, self.glyphPen, self.glyphBrush)
else:
self.addRect(glyph.x - 1, glyph.y - 1, glyph.width + 1, glyph.height + 1, self.spacePen, self.spaceBrush)
self.addLine(line.left, line.baseline, line.right, line.baseline, self.linePen)
def addPage(self, page):
qimage = page.image.toqimage()
graphicsitem = self.addPixmap(QPixmap.fromImage(qimage))
return graphicsitem
def highlightGlyph(self, glyph):
rect = self.addRect(glyph.x - 1, glyph.y - 1, glyph.width + 1, glyph.height + 1, self.glyphPen, self.glyphBrush)
rect.ensureVisible()
self.highlightedGlyphs[glyph] = rect
def clearHighlight(self):
for item in self.highlightedGlyphs.values():
self.removeItem(item)
self.highlightedGlyphs.clear()
class PageView(QGraphicsView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setFrameShape(self.NoFrame)