def on_selection_moved(self): selected_pixmap = self.get_selected_pixmap() self.art_shuffler.draw(selected_pixmap) self.sliced_pixmap_item.setPixmap(QPixmap.fromImage(self.sliced_image)) selected_pixmap = self.get_selected_pixmap() cell_width = (selected_pixmap.width() / self.selection_grid.column_count) cell_height = (selected_pixmap.height() / self.selection_grid.row_count) self.row_clues.clear() self.column_clues.clear() for i in range(self.selection_grid.row_count): clue_image = selected_pixmap.copy(0, i * cell_height, cell_width, cell_height) self.row_clues.append(clue_image) for j in range(self.selection_grid.column_count): clue_image = selected_pixmap.copy(j * cell_width, 0, cell_width, cell_height) self.column_clues.append(clue_image) self.symbols_shuffler.row_clues = self.row_clues self.symbols_shuffler.column_clues = self.column_clues self.symbols_shuffler.draw_grid(selected_pixmap) self.symbols_pixmap_item.setPixmap( QPixmap.fromImage(self.symbols_image)) self.timer.start()
def display_pic(self): ret, face = self.capture.read() self.frame = face frame = cv2.flip(cv2.cvtColor(face, cv2.COLOR_RGB2BGR), 1) image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888) self.label.setPixmap(QPixmap.fromImage(image))
def __init__(self, previewImage, fileName): super(ImageView, self).__init__() self.fileName = fileName mainLayout = QVBoxLayout(self) self.imageLabel = QLabel() self.imageLabel.setPixmap(QPixmap.fromImage(previewImage)) mainLayout.addWidget(self.imageLabel) topLayout = QHBoxLayout() self.fileNameLabel = QLabel(QDir.toNativeSeparators(fileName)) self.fileNameLabel.setTextInteractionFlags(Qt.TextBrowserInteraction) topLayout.addWidget(self.fileNameLabel) topLayout.addStretch() copyButton = QPushButton("Copy") copyButton.setToolTip("Copy file name to clipboard") topLayout.addWidget(copyButton) copyButton.clicked.connect(self.copy) launchButton = QPushButton("Launch") launchButton.setToolTip("Launch image viewer") topLayout.addWidget(launchButton) launchButton.clicked.connect(self.launch) mainLayout.addLayout(topLayout)
def view_cam(self): ret, self.image = self.cap.read() self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB) height, width, channel = self.image.shape step = channel * width self.rectangle_face() q_img = QImage(self.image.data, width, height, step, QImage.Format_RGB888) self.main_window.ui.label.setPixmap(QPixmap.fromImage(q_img))
def updatePixmap(self, image, scaleFactor): if not self.lastDragPos.isNull(): return self.pixmap = QPixmap.fromImage(image) self.pixmapOffset = QPoint() self.lastDragPosition = QPoint() self.pixmapScale = scaleFactor self.update()
def draw_preview_image(self, image: QImage) -> None: """ Draw the given image in the preview area. :type image: QImage """ image_preview = self.image_preview() image_preview.setPixmap( fit_to_frame(QPixmap.fromImage(image), QSize(image_preview.width(), image_preview.height())))
def convert_cv_qt(self, cv_img): """Convert from an opencv image to QPixmap""" rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) h, w, ch = rgb_image.shape bytes_per_line = ch * w convert_to_Qt_format = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888) p = convert_to_Qt_format.scaled(self.cam.width(), self.cam.height(), Qt.KeepAspectRatio) return QPixmap.fromImage(p)
def paint_with_opacity(pixmap: QPixmap, opacity: float): transparent_image = QImage(QSize(36, 36), QImage.Format_ARGB32_Premultiplied) transparent_image.fill(Qt.transparent) painter = QPainter(transparent_image) painter.setOpacity(opacity) painter.drawPixmap(18 - pixmap.width() / 2, 18 - pixmap.height() / 2, pixmap) painter.end() return QPixmap.fromImage(transparent_image)
def toqpixmap(im): # # This doesn't work. For now using a dumb approach. # im_data = _toqclass_helper(im) # result = QPixmap(im_data['im'].size[0], im_data['im'].size[1]) # result.loadFromData(im_data['data']) # Fix some strange bug that causes if im.mode == "RGB": im = im.convert("RGBA") qimage = toqimage(im) return QPixmap.fromImage(qimage)
def array3d_to_pixmap(array: np.ndarray) -> QPixmap: """ Convert a 3D array (color image) to a QPixmap. :param array: The array to convert. :return: QPixmap containing the array. """ assert array.ndim == 3 height, width, color_bytes = array.shape return QPixmap.fromImage( QImage(array.data, width, height, color_bytes * width, QImage.Format_BGR888))
def array2d_to_pixmap(array: np.ndarray, normalize=False, colormap: int = cv.COLORMAP_VIRIDIS) -> QPixmap: """ Convert a 2D array (monochrome image) to a QPixmap :param numpy.ndarray array: The array to convert. :param bool normalize: If `True` then apply colormap. :param colormap: Colomap used if normalize is `True` :return: QPixmap containing the array. """ assert array.ndim == 2 if normalize: array = apply_colormap(array, colormap) height, width, color_bytes = array.shape return QPixmap.fromImage( QImage(array.data, width, height, color_bytes * width, QImage.Format_BGR888)) height, width = array.shape return QPixmap.fromImage( QImage(array.data, width, height, width, QImage.Format_Grayscale8))
def assert_equal(self): __tracebackhide__ = True self.end() self.different_pixels = 0 actual_image: QImage = self.actual.device().toImage() expected_image: QImage = self.expected.device().toImage() diff_pixmap = QPixmap(actual_image.width(), actual_image.height()) diff = QPainter(diff_pixmap) try: white = QColor('white') diff.fillRect(0, 0, actual_image.width(), actual_image.height(), white) for x in range(actual_image.width()): for y in range(actual_image.height()): actual_colour = actual_image.pixelColor(x, y) expected_colour = expected_image.pixelColor(x, y) diff.setPen( self.diff_colour(actual_colour, expected_colour, x, y)) diff.drawPoint(x, y) finally: diff.end() diff_image: QImage = diff.device().toImage() display_diff(actual_image, diff_image, expected_image, self.different_pixels) if self.different_pixels == 0: return actual_image.save(str(self.work_dir / (self.name + '_actual.png'))) expected_image.save(str(self.work_dir / (self.name + '_expected.png'))) diff_path = self.work_dir / (self.name + '_diff.png') is_saved = diff_image.save(str(diff_path)) diff_width = self.diff_max_x - self.diff_min_x + 1 diff_height = self.diff_max_y - self.diff_min_y + 1 diff_section = QImage(diff_width, diff_height, QImage.Format_RGB32) diff_section_painter = QPainter(diff_section) try: diff_section_painter.drawPixmap(0, 0, diff_width, diff_height, QPixmap.fromImage(diff_image), self.diff_min_x, self.diff_min_y, diff_width, diff_height) finally: diff_section_painter.end() # To see an image dumped in the Travis CI log, copy the text from the # log, and paste it in test_pixmap_differ.test_decode_image. print(f'Encoded image of differing section ' f'({self.diff_min_x}, {self.diff_min_y}) - ' f'({self.diff_max_x}, {self.diff_max_y}):') print(encode_image(diff_section)) message = f'Found {self.different_pixels} different pixels, ' message += f'see' if is_saved else 'could not write' message += f' {diff_path.relative_to(Path(__file__).parent.parent)}.' assert self.different_pixels == 0, message
def __init__(self, parent: Optional[QWidget] = None, size: QSize = QSize(600, 600), image: Optional[QImage] = None): super(CLusterPreviewWindow, self).__init__(parent) self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint) self.resize(size) self.imageLabel = QLabel("Cluster Preview", self) self.imageLabel.setAlignment(Qt.AlignCenter) layout = QGridLayout(self) layout.addWidget(self.imageLabel) if image is not None: self.__update_cluster_preview(QPixmap.fromImage(image))
def update_frame_image(self, frame_index: int): """ 刷新帧图象显示 :param frame_index: 帧索引 :return: None """ base64 = self._frame_base64_dict[frame_index] byte_arr = QByteArray(base64) img = QImage() img.loadFromData(QByteArray.fromBase64(byte_arr)) pixmap = QPixmap.fromImage(img) self.current_frame_item.setPixmap(pixmap) self.current_frame_index = frame_index
def update_cluster_preview(self, image: Union[np.ndarray, str]) -> None: """ Load an image from a string or an array and update the cluster preview. :param image: Can be both a numpy array and a string. """ if isinstance(image, np.ndarray): self.__update_cluster_preview(array2d_to_pixmap(image, normalize=True, colormap=cv.COLORMAP_JET)) return if isinstance(image, str): self.__update_cluster_preview(QPixmap.fromImage(QImage(image))) return raise ValueError("Invalid image type: {}".format(type(image)))
def assert_equal(self): __tracebackhide__ = True self.end() self.different_pixels = 0 actual_image: QImage = self.actual.device().toImage() expected_image: QImage = self.expected.device().toImage() diff_pixmap = QPixmap(actual_image.width(), actual_image.height()) diff = QPainter(diff_pixmap) try: white = QColor('white') diff.fillRect(0, 0, actual_image.width(), actual_image.height(), white) for x in range(actual_image.width()): for y in range(actual_image.height()): actual_colour = actual_image.pixelColor(x, y) expected_colour = expected_image.pixelColor(x, y) diff.setPen( self.diff_colour(actual_colour, expected_colour, x, y)) diff.drawPoint(x, y) finally: diff.end() diff_image: QImage = diff.device().toImage() display_diff(actual_image, diff_image, expected_image, self.different_pixels) if self.different_pixels == 0: return actual_image.save(str(self.work_dir / (self.name + '_actual.png'))) expected_image.save(str(self.work_dir / (self.name + '_expected.png'))) diff_path = self.work_dir / (self.name + '_diff.png') is_saved = diff_image.save(str(diff_path)) diff_width = self.diff_max_x - self.diff_min_x + 1 diff_height = self.diff_max_y - self.diff_min_y + 1 diff_section = QImage(diff_width, diff_height, QImage.Format_RGB32) diff_section_painter = QPainter(diff_section) try: diff_section_painter.drawPixmap(0, 0, diff_width, diff_height, QPixmap.fromImage(diff_image), self.diff_min_x, self.diff_min_y, diff_width, diff_height) finally: diff_section_painter.end() message = f'Found {self.different_pixels} different pixels.' assert self.different_pixels == 0, message
def _on_calculate_word_freq(self): # one reference # selected = self.window.refListView.currentIndex().row() # refPath = self.refPaths[selected] selecteds = self.window.refListView.selectedIndexes() selectedRefPaths = [] for selected in selecteds: selectedRefPaths.append(self.refPaths[selected.row()]) self.backend.update_refs(selectedRefPaths) wordCloudDraw = self.backend.drawWordCloud() # binary biWordCloudDraw = wordCloudDraw.to_array() x = biWordCloudDraw.shape[1] y = biWordCloudDraw.shape[0] channel = biWordCloudDraw.shape[2] frame = QImage(biWordCloudDraw, x, y, QImage.Format_RGB888) pix = QPixmap.fromImage(frame) scene = QGraphicsScene() item = QGraphicsPixmapItem(pix) scene.addItem(item) self.window.wordCloudView.setScene(scene) def fit_view(): self.window.wordCloudView.fitInView(item) def save_image(): wordCloudDraw.to_file('wc.png') rezoom = QAction(self.window.wordCloudView) rezoom.setText('Rezoom') rezoom.triggered.connect(fit_view) save = QAction(self.window.wordCloudView) save.setText('Save') save.triggered.connect(save_image) self.window.wordCloudView.addAction(rezoom) self.window.wordCloudView.addAction(save)
def __init__(self, parent: QWidget, image: QImage, name: str, image_path: Optional[str] = None, array_path: Optional[str] = None): """ Create an ImageEntry :param QWidget parent: The parent containing the ImageEntry. :param QImage image: The image that will be draw in the thumbnail. :param str name: The name that will be shown below the thumbnail. Also used for image basename. :param image_path: Path to the image file. :type image_path: str, optional :param array_path: Path to the Numpy array file. :type array_path: str, optional """ super(ImageEntry, self).__init__(parent) self.__selected = False self.__mouse_pressed_handlers: List[Callable[[ImageEntry, QMouseEvent], Any]] = [] self.setAutoFillBackground(True) self.__default_background_color = self.palette().color( self.backgroundRole()) self.image_path: Final[Optional[str]] = image_path self.array_path: Final[Optional[str]] = array_path self.basename: Final[str] = name thumbnail = QLabel("Thumbnail", self) thumbnail.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding) thumbnail.setAlignment(Qt.AlignCenter) thumbnail.setPixmap( fit_to_frame(QPixmap.fromImage(image), QSize(50, 50))) name_label = QLabel('\n'.join(wrap(name, 15)), self) name_label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum) name_label.setAlignment(Qt.AlignCenter) layout = QVBoxLayout(self) layout.addWidget(thumbnail, alignment=Qt.AlignHCenter) layout.addWidget(name_label, alignment=Qt.AlignHCenter) self.setLayout(layout)
def setImage(self, image): self.label.setPixmap(QPixmap.fromImage(image))
def pixmap(self): return QPixmap.fromImage(self._image)
def media_status(self): """Checks the queue for a new image to display.""" media_size = self.inter.media.contentsRect() self.client.media_size = media_size.width(), media_size.height() if self.client.media is not None: self.inter.media.setPixmap(QPixmap.fromImage(self.client.media))
def scale_image(self): if self.pixmap is None: return if self.selection_grid is None: x = self.settings.value('x', 0.0, float) y = self.settings.value('y', 0.0, float) width = self.settings.value('width', 1.0, float) height = self.settings.value('height', 1.0, float) else: x, y, width, height = self.get_selected_fraction() self.art_scene.clear() self.cells.clear() view_size = self.ui.art_view.maximumViewportSize() if view_size.width() == 0: return self.art_scene.setSceneRect(0, 0, view_size.width(), view_size.height()) display_size = QSize(view_size.width() * 0.99 / 2, view_size.height() * 0.99) self.scaled_pixmap = self.pixmap.scaled( display_size, aspectMode=Qt.AspectRatioMode.KeepAspectRatio) self.art_scene.addPixmap(self.scaled_pixmap) scaled_size = self.scaled_pixmap.size() self.selection_grid = SelectionGrid(scaled_size.width() * x, scaled_size.height() * y, scaled_size.width() * width, scaled_size.height() * height, row_count=self.row_count, column_count=self.column_count) self.selection_grid.on_moved = self.on_selection_moved self.art_scene.addItem(self.selection_grid) self.sliced_image = QImage(display_size, QImage.Format.Format_ARGB32_Premultiplied) self.check_clues() self.art_shuffler = ArtShuffler(self.selection_grid.row_count, self.selection_grid.column_count, self.sliced_image, QRect(0, 0, display_size.width(), display_size.height()), clues=self.clues, row_clues=self.row_clues, column_clues=self.column_clues) self.sliced_pixmap_item = self.art_scene.addPixmap( QPixmap.fromImage(self.sliced_image)) self.sliced_pixmap_item.setPos(display_size.width(), 0) self.symbols_scene.clear() self.symbols_source_pixmap_item = self.symbols_scene.addPixmap( self.scaled_pixmap) self.symbols_image = QImage(display_size, QImage.Format.Format_ARGB32_Premultiplied) if self.symbols_shuffler is not None: selected_row = self.symbols_shuffler.selected_row selected_column = self.symbols_shuffler.selected_column else: selected_row = 0 selected_column = None self.symbols_shuffler = ArtShuffler(self.selection_grid.row_count, self.selection_grid.column_count, self.symbols_image, QRect(0, 0, display_size.width(), display_size.height()), row_clues=self.row_clues, column_clues=self.column_clues) self.symbols_shuffler.selected_row = selected_row self.symbols_shuffler.selected_column = selected_column self.symbols_pixmap_item = ClickablePixmapItem( QPixmap.fromImage(self.symbols_image)) self.symbols_pixmap_item.on_click = self.on_symbols_clicked self.symbols_scene.addItem(self.symbols_pixmap_item) self.symbols_pixmap_item.setPos(display_size.width(), 0) self.on_selection_moved()
def cvimgToQPixmap(cvImage) : cvImage=cv2.resize(src=cvImage,dsize=None,fx=0.2,fy=0.2) cvImage=cv2.cvtColor(cvImage,cv2.COLOR_BGR2RGB) image = QImage(cvImage[:], cvImage.shape[1], cvImage.shape[0], cvImage.shape[1] * cvImage.shape[2], QImage.Format_RGB888) pixmap = QPixmap.fromImage(image) return pixmap