SHARE
TWEET

imageviewer

a guest Nov 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. from PyQt5.QtCore import QDir, Qt
  3. from PyQt5.QtGui import QImage, QPainter, QPalette, QPixmap
  4. from PyQt5.QtWidgets import (QAction, QApplication, QFileDialog, QLabel,
  5.         QMainWindow, QMenu, QMessageBox, QScrollArea, QSizePolicy)
  6. from PyQt5.QtPrintSupport import QPrintDialog, QPrinter
  7.  
  8.  
  9. class ImageViewer(QMainWindow):
  10.     def __init__(self):
  11.         super(ImageViewer, self).__init__()
  12.         self.keras_image=keras_image
  13.        
  14.         self.printer = QPrinter()
  15.         self.scaleFactor = 0.0
  16.  
  17.         self.imageLabel = QLabel()
  18.         self.imageLabel.setBackgroundRole(QPalette.Base)
  19.         self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
  20.         self.imageLabel.setScaledContents(True)
  21.  
  22.         self.scrollArea = QScrollArea()
  23.         self.scrollArea.setBackgroundRole(QPalette.Dark)
  24.         self.scrollArea.setWidget(self.imageLabel)
  25.         self.setCentralWidget(self.scrollArea)
  26.  
  27.         self.createActions()
  28.         self.createMenus()
  29.  
  30.         self.setWindowTitle("Image Viewer")
  31.         self.resize(500, 400)
  32.  
  33.     def open(self):
  34.         fileName, _ = QFileDialog.getOpenFileName(self, "Open File",
  35.                 QDir.currentPath())
  36.         if fileName:
  37.             image = QImage(fileName)
  38.             if image.isNull():
  39.                 QMessageBox.information(self, "Image Viewer",
  40.                         "Cannot load %s." % fileName)
  41.                 return
  42.  
  43.             self.imageLabel.setPixmap(QPixmap.fromImage(image))
  44.             self.scaleFactor = 1.0
  45.  
  46.             self.printAct.setEnabled(True)
  47.             self.fitToWindowAct.setEnabled(True)
  48.             self.updateActions()
  49.  
  50.             if not self.fitToWindowAct.isChecked():
  51.                 self.imageLabel.adjustSize()
  52.            
  53.             i = keras_image.load_img(filename, target_size=(64,64))
  54.             i = np.expand_dims(i, axis=0)
  55.             result = model.predict(i)
  56.             if result[0][0] == 1:
  57.                 self.setWindowTitle('dog')
  58.             else:
  59.                 self.setWindowTitle('cat')
  60.            
  61.     def print_(self):
  62.         dialog = QPrintDialog(self.printer, self)
  63.         if dialog.exec_():
  64.             painter = QPainter(self.printer)
  65.             rect = painter.viewport()
  66.             size = self.imageLabel.pixmap().size()
  67.             size.scale(rect.size(), Qt.KeepAspectRatio)
  68.             painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
  69.             painter.setWindow(self.imageLabel.pixmap().rect())
  70.             painter.drawPixmap(0, 0, self.imageLabel.pixmap())
  71.  
  72.     def zoomIn(self):
  73.         self.scaleImage(1.25)
  74.  
  75.     def zoomOut(self):
  76.         self.scaleImage(0.8)
  77.  
  78.     def normalSize(self):
  79.         self.imageLabel.adjustSize()
  80.         self.scaleFactor = 1.0
  81.  
  82.     def fitToWindow(self):
  83.         fitToWindow = self.fitToWindowAct.isChecked()
  84.         self.scrollArea.setWidgetResizable(fitToWindow)
  85.         if not fitToWindow:
  86.             self.normalSize()
  87.  
  88.         self.updateActions()
  89.  
  90.     def about(self):
  91.         QMessageBox.about(self, "About Image Viewer",
  92.                 "<p>The <b>Image Viewer</b> example shows how to combine "
  93.                 "QLabel and QScrollArea to display an image. QLabel is "
  94.                 "typically used for displaying text, but it can also display "
  95.                 "an image. QScrollArea provides a scrolling view around "
  96.                 "another widget. If the child widget exceeds the size of the "
  97.                 "frame, QScrollArea automatically provides scroll bars.</p>"
  98.                 "<p>The example demonstrates how QLabel's ability to scale "
  99.                 "its contents (QLabel.scaledContents), and QScrollArea's "
  100.                 "ability to automatically resize its contents "
  101.                 "(QScrollArea.widgetResizable), can be used to implement "
  102.                 "zooming and scaling features.</p>"
  103.                 "<p>In addition the example shows how to use QPainter to "
  104.                 "print an image.</p>")
  105.  
  106.     def createActions(self):
  107.         self.openAct = QAction("&Open...", self, shortcut="Ctrl+O",
  108.                 triggered=self.open)
  109.  
  110.         self.printAct = QAction("&Print...", self, shortcut="Ctrl+P",
  111.                 enabled=False, triggered=self.print_)
  112.  
  113.         self.exitAct = QAction("E&xit", self, shortcut="Ctrl+Q",
  114.                 triggered=self.close)
  115.  
  116.         self.zoomInAct = QAction("Zoom &In (25%)", self, shortcut="Ctrl++",
  117.                 enabled=False, triggered=self.zoomIn)
  118.  
  119.         self.zoomOutAct = QAction("Zoom &Out (25%)", self, shortcut="Ctrl+-",
  120.                 enabled=False, triggered=self.zoomOut)
  121.  
  122.         self.normalSizeAct = QAction("&Normal Size", self, shortcut="Ctrl+S",
  123.                 enabled=False, triggered=self.normalSize)
  124.  
  125.         self.fitToWindowAct = QAction("&Fit to Window", self, enabled=False,
  126.                 checkable=True, shortcut="Ctrl+F", triggered=self.fitToWindow)
  127.  
  128.         self.aboutAct = QAction("&About", self, triggered=self.about)
  129.  
  130.         self.aboutQtAct = QAction("About &Qt", self,
  131.                 triggered=QApplication.instance().aboutQt)
  132.  
  133.     def createMenus(self):
  134.         self.fileMenu = QMenu("&File", self)
  135.         self.fileMenu.addAction(self.openAct)
  136.         self.fileMenu.addAction(self.printAct)
  137.         self.fileMenu.addSeparator()
  138.         self.fileMenu.addAction(self.exitAct)
  139.  
  140.         self.viewMenu = QMenu("&View", self)
  141.         self.viewMenu.addAction(self.zoomInAct)
  142.         self.viewMenu.addAction(self.zoomOutAct)
  143.         self.viewMenu.addAction(self.normalSizeAct)
  144.         self.viewMenu.addSeparator()
  145.         self.viewMenu.addAction(self.fitToWindowAct)
  146.  
  147.         self.helpMenu = QMenu("&Help", self)
  148.         self.helpMenu.addAction(self.aboutAct)
  149.         self.helpMenu.addAction(self.aboutQtAct)
  150.  
  151.         self.menuBar().addMenu(self.fileMenu)
  152.         self.menuBar().addMenu(self.viewMenu)
  153.         self.menuBar().addMenu(self.helpMenu)
  154.  
  155.     def updateActions(self):
  156.         self.zoomInAct.setEnabled(not self.fitToWindowAct.isChecked())
  157.         self.zoomOutAct.setEnabled(not self.fitToWindowAct.isChecked())
  158.         self.normalSizeAct.setEnabled(not self.fitToWindowAct.isChecked())
  159.  
  160.     def scaleImage(self, factor):
  161.         self.scaleFactor *= factor
  162.         self.imageLabel.resize(self.scaleFactor * self.imageLabel.pixmap().size())
  163.  
  164.         self.adjustScrollBar(self.scrollArea.horizontalScrollBar(), factor)
  165.         self.adjustScrollBar(self.scrollArea.verticalScrollBar(), factor)
  166.  
  167.         self.zoomInAct.setEnabled(self.scaleFactor < 3.0)
  168.         self.zoomOutAct.setEnabled(self.scaleFactor > 0.333)
  169.  
  170.     def adjustScrollBar(self, scrollBar, factor):
  171.         scrollBar.setValue(int(factor * scrollBar.value()
  172.                                 + ((factor - 1) * scrollBar.pageStep()/2)))
  173.  
  174.  
  175. if __name__ == '__main__':
  176.  
  177.     import sys
  178.  
  179.     app = QApplication(sys.argv)
  180.     imageViewer = ImageViewer()
  181.     imageViewer.show()
  182.     sys.exit(app.exec_())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top