From e9f034f54b3131b89f4fc4da57098105ec193403 Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Sun, 10 Aug 2014 22:57:08 +0200 Subject: [PATCH] Add proof-of-concept GUI. --- pixelocr/gui/__init__.py | 43 ++++++++++++++++++++++ pixelocr/gui/pageview.py | 78 ++++++++++++++++++++++++++++++++++++++++ pixelocr/gui/window.py | 64 +++++++++++++++++++++++++++++++++ setup.py | 3 ++ 4 files changed, 188 insertions(+) create mode 100644 pixelocr/gui/__init__.py create mode 100644 pixelocr/gui/pageview.py create mode 100644 pixelocr/gui/window.py diff --git a/pixelocr/gui/__init__.py b/pixelocr/gui/__init__.py new file mode 100644 index 0000000..257eb3d --- /dev/null +++ b/pixelocr/gui/__init__.py @@ -0,0 +1,43 @@ +# 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 . + + +import sys + +import sip +sip.setapi('QString', 2) + +from .window import MainWindow + + +from PyQt4.QtGui import ( + QApplication, +) + + +def main(): + app = QApplication(sys.argv) + + QApplication.setOrganizationName("PixelOCR"); + QApplication.setApplicationName("PixelOCR"); + + win = MainWindow() + win.show() + + from ..page import Page + from ..image import Image + win.pageScene.setPage(Page(Image.fromfile(sys.argv[1]).unframe(10))) + + sys.exit(app.exec_()) diff --git a/pixelocr/gui/pageview.py b/pixelocr/gui/pageview.py new file mode 100644 index 0000000..8aeab6b --- /dev/null +++ b/pixelocr/gui/pageview.py @@ -0,0 +1,78 @@ +# 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 . + + +from PyQt4.QtCore import Qt +from PyQt4.QtGui import ( + QImage, + QPixmap, + QGraphicsView, + QGraphicsScene, + QGraphicsDropShadowEffect, + QPen, + QBrush, + QColor, + QPalette, +) + +import numpy as np +from scipy.ndimage import imread + + +def ndimage2qimage(image): + return QImage( + np.ascontiguousarray(image.astype(np.uint8).copy()).data, + image.shape[1], image.shape[0], + image.shape[1] * 3, + QImage.Format_RGB888, + ) + + +class PageScene(QGraphicsScene): + page = None + pageItem = None + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.setBackgroundBrush(self.palette().window()) + + def setPage(self, page): + for item in self.items(): + self.remoteItem(item) + self.page = page + self.pageItem = self.addPage(page) + + shadow = QGraphicsDropShadowEffect() + shadow.setBlurRadius(20) + shadow.setOffset(2, 2) + self.pageItem.setGraphicsEffect(shadow) + + letterPen = QPen(QColor(50, 50, 50, 100)) + letterBrush = QBrush(QColor(255, 255, 0, 60)) + linePen = QPen(QColor(255, 150, 150, 100)) + for line in page: + for letter in line: + 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) + + def addPage(self, page): + qimage = ndimage2qimage(page.image.data) + graphicsitem = self.addPixmap(QPixmap.fromImage(qimage)) + return graphicsitem + + +class PageView(QGraphicsView): + pass diff --git a/pixelocr/gui/window.py b/pixelocr/gui/window.py new file mode 100644 index 0000000..b3644b2 --- /dev/null +++ b/pixelocr/gui/window.py @@ -0,0 +1,64 @@ +# 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 . + + +from PyQt4.QtCore import ( + Qt, + QSettings, + QSize, +) + +from PyQt4.QtGui import ( + QMainWindow, + QPushButton, + QVBoxLayout, + QWidget, +) + +from .pageview import PageScene, PageView + + +class MainWindow(QMainWindow): + def __init__(self): + super().__init__() + self.setWindowTitle('PixelOCR') + + centralWidget = QWidget(self) + self.setCentralWidget(centralWidget) + self.pageScene = PageScene(self) + self.page = PageView(self.pageScene, centralWidget) + + layout = QVBoxLayout(centralWidget) + layout.addWidget(self.page) + + self.readSettings() + + def sizeHint(self): + return QSize(800, 600) + + def closeEvent(self, event): + self.saveSettings() + super().closeEvent(event) + + def saveSettings(self): + settings = QSettings() + settings.setValue('windowGeometry', self.saveGeometry()) + settings.setValue('windowState', self.saveState()) + + def readSettings(self): + settings = QSettings() + self.restoreGeometry(settings.value("windowGeometry") or b'') + self.restoreState(settings.value("windowState") or b'') + diff --git a/setup.py b/setup.py index bf5470a..63f97bd 100755 --- a/setup.py +++ b/setup.py @@ -17,5 +17,8 @@ setup( 'Intended Audience :: End Users/Desktop', 'Operating System :: OS Independent', ], + entry_points={ + 'console_scripts': ['pixelocr = pixelocr.gui:main'] + }, packages=find_packages(), )