def check_image(self, filename): """ Checks a filename to see if the image can be loaded. Parameters ---------- filename : (str) Inputted filename by user Returns ------- tuple : (str, misc) Error message and None if an error is present or None and a QSvgRenderer/QPixmap (depending on file type). """ error = None file_type = None abs_path = os.path.expanduser(os.path.expandvars(filename)) if not os.path.isabs(abs_path): try: if is_qt_designer(): p = self.get_designer_window() if p is not None: ui_dir = p.absoluteDir().absolutePath() abs_path = os.path.join(ui_dir, abs_path) else: parent_display = self.widget.find_parent_display() base_path = None if parent_display: base_path = os.path.dirname( parent_display.loaded_file()) abs_path = find_file(abs_path, base_path=base_path) except Exception as ex: print("Exception: ", ex) error = "Unable to find full filepath for {}".format(filename) abs_path = filename # First, lets try SVG. We have to try SVG first, otherwise # QPixmap will happily load the SVG and turn it into a raster image. # Really annoying: We have to try to load the file as SVG, # and we expect it will fail often (because many images aren't SVG). # Qt prints a warning message to stdout any time SVG loading fails. # So we have to temporarily silence Qt warning messages here. qInstallMessageHandler(self.qt_message_handler) svg = QSvgRenderer() if svg.load(abs_path): file_type = svg qInstallMessageHandler(None) return error, file_type qInstallMessageHandler(None) # SVG didn't work, lets try QPixmap image = QPixmap(abs_path) if not image.isNull(): file_type = image return error, file_type # If we get this far, the file specified could not be loaded at all. if error is None: error = "Could not load image \n{}".format(filename) return error, file_type
def imageFiles(self, new_files): """ JSON-formatted dictionary keyed on states (integers), with filenames of the image file to display for the state. Parameters ---------- new_files : str """ self._state_images_string = str(new_files) try: new_file_dict = json.loads(self._state_images_string) except Exception: self._state_images = {} return self._sizeHint = QSize(0, 0) for (state, filename) in new_file_dict.items(): if is_pydm_app(): try: file_path = self.app.get_path(filename) except Exception as e: logger.exception("Couldn't get file with path %s", filename) file_path = filename else: file_path = filename # First, lets try SVG. We have to try SVG first, otherwise # QPixmap will happily load the SVG and turn it into a raster image. # Really annoying: We have to try to load the file as SVG, # and we expect it will fail often (because many images aren't SVG). # Qt prints a warning message to stdout any time SVG loading fails. # So we have to temporarily silence Qt warning messages here. qInstallMessageHandler(self.qt_message_handler) svg = QSvgRenderer() svg.repaintNeeded.connect(self.update) if svg.load(file_path): self._state_images[int(state)] = (filename, svg) self._sizeHint = self._sizeHint.expandedTo(svg.defaultSize()) qInstallMessageHandler(None) continue qInstallMessageHandler(None) # SVG didn't work, lets try QPixmap image = QPixmap(file_path) if not image.isNull(): self._state_images[int(state)] = (filename, image) self._sizeHint = self._sizeHint.expandedTo(image.size()) continue # If we get this far, the file specified could not be loaded at all. logger.error("Could not load image: {}".format(filename)) self._state_images[int(state)] = (filename, None)
def imageFiles(self, new_files): """ JSON-formatted dictionary keyed on states (integers), with filenames of the image file to display for the state. Parameters ---------- new_files : str """ self._state_images_string = str(new_files) try: new_file_dict = json.loads(self._state_images_string) except Exception: self._state_images = {} return self._sizeHint = QSize(0, 0) for (state, filename) in new_file_dict.items(): if is_pydm_app(): try: file_path = self.app.get_path(filename) except Exception as e: logger.exception("Couldn't get file with path %s", filename) file_path = filename else: file_path = filename # First, lets try SVG. We have to try SVG first, otherwise # QPixmap will happily load the SVG and turn it into a raster image. # Really annoying: We have to try to load the file as SVG, # and we expect it will fail often (because many images aren't SVG). # Qt prints a warning message to stdout any time SVG loading fails. # So we have to temporarily silence Qt warning messages here. qInstallMessageHandler(self.qt_message_handler) svg = QSvgRenderer() svg.repaintNeeded.connect(self.update) if svg.load(file_path): self._state_images[int(state)] = (filename, svg) self._sizeHint = self._sizeHint.expandedTo(svg.defaultSize()) qInstallMessageHandler(None) continue qInstallMessageHandler(None) # SVG didn't work, lets try QPixmap image = QPixmap(file_path) if not image.isNull(): self._state_images[int(state)] = (filename, image) self._sizeHint = self._sizeHint.expandedTo(image.size()) continue # If we get this far, the file specified could not be loaded at all. logger.error("Could not load image: {}".format(filename)) self._state_images[int(state)] = (filename, None)
def hough_load_btn(self): try: options = QFileDialog.Options() self.hough_fileName, _ = QFileDialog.getOpenFileName( None, 'Upload Image', '', '*.png *.jpg *.jpeg', options=options) pixmap = QPixmap(self.hough_fileName) if (not pixmap.isNull()): pixmap = pixmap.scaled(self.label_hough_in.width(), self.label_hough_in.height(), QtCore.Qt.KeepAspectRatio) self.label_hough_in.setPixmap(pixmap) except Exception as err: print(err)
def sift_imgB_load(self): try: options = QFileDialog.Options() self.siftB, _ = QFileDialog.getOpenFileName(None, 'Upload Image', '', '*.png *.jpg *.jpeg', options=options) pixmap = QPixmap(self.siftB) if (not pixmap.isNull()): pixmap = pixmap.scaled(self.label_input_SIFT_B.width(), self.label_input_SIFT_B.height(), QtCore.Qt.KeepAspectRatio) self.label_input_SIFT_B.setPixmap(pixmap) except Exception as err: print(err)