Example #1
0
 def _pil_to_qt(self, image):
     if image.mode == "RGB":
         r, g, b = image.split()
         image = Image.merge("RGB", (b, g, r))
     elif image.mode == "RGBA":
         r, g, b, a = image.split()
         image = Image.merge("RGBA", (b, g, r, a))
     elif image.mode == "L":
         image = image.convert("RGBA")
     converted_image = image.convert("RGBA")
     data = converted_image.tobytes("raw", "RGBA")
     qim = qtg.QImage(data, image.size[0], image.size[1],
                      qtg.QImage.Format.Format_ARGB32)
     pixmap = qtg.QPixmap.fromImage(qim)
     return pixmap
Example #2
0
 def onTimerGetPage(self):
     try:
         ret = self.queDoc.get(False)
         if isinstance(ret, int):
             self.timerWaiting.stop()
             self.page_count = ret
             self.label.setText("{}/{}".format(self.curPageNum + 1, self.page_count))
         else:  # tuple, pixmap info
             num, samples, width, height, stride, alpha = ret
             self.curPageNum = num
             self.label.setText("{}/{}".format(self.curPageNum + 1, self.page_count))
             fmt = (
                 QtGui.QImage.Format.Format_RGBA8888
                 if alpha
                 else QtGui.QImage.Format.Format_RGB888
             )
             qimg = QtGui.QImage(samples, width, height, stride, fmt)
             self.labelImg.setPixmap(QtGui.QPixmap.fromImage(qimg))
     except queue.Empty as ex:
         pass
Example #3
0
    def load_image(self, in_file_name):
        Image.MAX_IMAGE_PIXELS = 1000000000
        file_name = in_file_name
        if type(file_name) == QtCore.QUrl:
            file_name = in_file_name.toLocalFile()

        if self.directory == '':
            self.directory = os.path.split(file_name)[0]
            self.directory_set.emit(self.directory)

        if self.directory == os.path.split(file_name)[0]:
            QtWidgets.QApplication.setOverrideCursor(
                QtCore.Qt.CursorShape.WaitCursor)
            self.selection = []
            self.clear()
            self.current_image_name = os.path.split(file_name)[1]
            if self.current_image_name not in self.points:
                self.points[self.current_image_name] = {}
            try:
                img = Image.open(file_name)
                channels = len(img.getbands())
                array = np.array(img)
                img.close()
                if array.shape[0] > 10000 or array.shape[1] > 10000:
                    # Make smaller tiles to save memory
                    stride = 100
                    max_stride = (array.shape[1] // stride) * stride
                    tail = array.shape[1] - max_stride
                    tile = np.zeros((array.shape[0], stride, array.shape[2]),
                                    dtype=np.uint8)
                    for s in range(0, max_stride, stride):
                        tile[:, :] = array[:, s:s + stride]
                        qt_image = QtGui.QImage(
                            tile.data, tile.shape[1], tile.shape[0],
                            QtGui.QImage.Format.Format_RGB888)
                        pixmap = QtGui.QPixmap.fromImage(qt_image)
                        item = self.addPixmap(pixmap)
                        item.moveBy(s, 0)
                    # Fix for windows, thin slivers at the end cause the app to hang. QImage bug?
                    if tail > 0:
                        tile2 = np.ones(
                            (array.shape[0], stride, array.shape[2]),
                            dtype=np.uint8) * 255
                        tile2[:, 0:tail] = array[:, max_stride:array.shape[1]]
                        qt_image = QtGui.QImage(
                            tile2.data, tile2.shape[1], tile2.shape[0],
                            QtGui.QImage.Format.Format_RGB888)
                        pixmap = QtGui.QPixmap.fromImage(qt_image)
                        item = self.addPixmap(pixmap)
                        item.moveBy(max_stride, 0)
                else:
                    if channels == 1:
                        self.qt_image = QtGui.QImage(
                            array.data, array.shape[1], array.shape[0],
                            QtGui.QImage.Format.Format_Grayscale8)
                    else:
                        # Apply basic min max stretch to the image
                        for chan in range(channels):
                            array[:, :,
                                  chan] = np.interp(array[:, :, chan],
                                                    (array[:, :, chan].min(),
                                                     array[:, :, chan].max()),
                                                    (0, 255))
                        bpl = int(array.nbytes / array.shape[0])
                        if array.shape[2] == 4:
                            self.qt_image = QtGui.QImage(
                                array.data, array.shape[1], array.shape[0],
                                QtGui.QImage.Format.Format_RGBA8888)
                        else:
                            self.qt_image = QtGui.QImage(
                                array.data, array.shape[1], array.shape[0],
                                bpl, QtGui.QImage.Format.Format_RGB888)
                    self.pixmap = QtGui.QPixmap.fromImage(self.qt_image)
                    self.addPixmap(self.pixmap)
            except FileNotFoundError:
                QtWidgets.QMessageBox.critical(
                    None, 'File Not Found',
                    '{} is not in the same folder as the point file.'.format(
                        self.current_image_name))
                self.image_loaded.emit(self.directory, self.current_image_name)
            self.image_loaded.emit(self.directory, self.current_image_name)
            self.display_points()
            self.display_grid()
            QtWidgets.QApplication.restoreOverrideCursor()