class QtReader(BaseReader): """Image reader using Qt's QImageReader implementation under the hood.""" def __init__(self, path: str, file_format: str): super().__init__(path, file_format) self._handler = QImageReader(path, file_format.encode()) self._handler.setAutoTransform(True) if not self._handler.canRead(): # TODO raise ValueError(f"'{path}' cannot be read as image") @classmethod def supports(cls, file_format: str) -> bool: return file_format in QImageReader.supportedImageFormats() @property def is_animation(self) -> bool: return self._handler.supportsAnimation() def get_pixmap(self) -> QPixmap: """Retrieve the pixmap directly from the image reader.""" pixmap = QPixmap.fromImageReader(self._handler) if self._handler.error(): raise ValueError( f"Error reading image '{self.path}': {self._handler.errorString()}" ) return pixmap def get_image(self, size: int) -> QImage: """Retrieve the down-scaled image directly from the image reader.""" qsize = self._handler.size() qsize.scale(size, size, Qt.KeepAspectRatio) self._handler.setScaledSize(qsize) return self._handler.read()
def _plat_get_dimensions(self): try: ir = QImageReader(str(self.path)) size = ir.size() if size.isValid(): return (size.width(), size.height()) else: return (0, 0) except EnvironmentError: logging.warning("Could not read image '%s'", str(self.path)) return (0, 0)
def accept(self): for key, field in self.fields.items(): self.data[key] = field["edit"].text() if self.data["name"] == "": msgBox = QMessageBox() msgBox.setText("Please, enter a name for the map.") msgBox.exec() return # check if the RGB map file exists rgb_filename = self.data['rgb_filename'] if not os.path.exists(rgb_filename): msgBox = QMessageBox() msgBox.setText("The RGB image file does not seems to exist.") msgBox.exec() return # check if the depth map file exists depth_filename = self.data['depth_filename'] if not os.path.exists(rgb_filename): msgBox = QMessageBox() msgBox.setText("The depth map file does not seems to exist.") msgBox.exec() return # check validity of the acquisition date txt = self.data["acquisition_date"] if not utils.isValidDate(txt): msgBox = QMessageBox() msgBox.setText( "Invalid date format. Please, enter the acquisition date as YYYY-MM-DD." ) msgBox.exec() return # TODO: redundat check, remove it ? image_reader = QImageReader(rgb_filename) size = image_reader.size() if size.width() > 32767 or size.height() > 32767: msgBox = QMessageBox() msgBox.setText( "The image is too big. TagLab is limited to 32767x32767 pixels." ) msgBox.exec() return self.accepted.emit() self.close()
def _create_thumbnail(self, path: str, thumbnail_path: str) -> QPixmap: """Create thumbnail for an image. Args: path: Path to the image for which the thumbnail is created. thumbnail_path: Path to which the thumbnail is stored. Returns: The created QPixmap. """ # Cannot access source if not os.access(path, os.R_OK): return self._manager.fail_pixmap size = 256 if self._manager.large else 128 reader = QImageReader(path) reader.setAutoTransform(True) # Automatically apply exif orientation if reader.canRead(): qsize = reader.size() qsize.scale(size, size, Qt.KeepAspectRatio) reader.setScaledSize(qsize) image = reader.read() # Image was deleted in the time between reader.read() and now try: attributes = self._get_thumbnail_attributes(path, image) except FileNotFoundError: return self._manager.fail_pixmap for key, value in attributes.items(): image.setText(key, value) # First create temporary file and then move it. This avoids # problems with concurrent access of the thumbnail cache, since # "move" is an atomic operation handle, tmp_filename = tempfile.mkstemp( dir=self._manager.directory) os.close(handle) os.chmod(tmp_filename, 0o600) image.save(tmp_filename, format="png") os.replace(tmp_filename, thumbnail_path) return QPixmap(image) return self._manager.fail_pixmap
def get_archive_wallpapers(self): """ Generator returning the date, path and copyright info (via db lookup) of each file found in the archive location. :rtype: QDate, unicode, unicode """ image_reader = QImageReader() regex_date_split = re.compile(r'(\d{4})(\d{2})(\d{2})') copyright_info = self.copyright_db.get_all_info() archive_folder = self.settings.archive_location for filename in reversed([ x for x in os.listdir(archive_folder) if re_archive_file.match(x) ]): year, month, day = list( map(int, regex_date_split.findall(filename)[0])) wallpaper_date = QDate(year, month, day) wallpaper_copyright = copyright_info.get( '{0:04d}-{1:02d}-{2:02d}'.format(year, month, day), '') wallpaper_filename = os.path.join(str(archive_folder), filename) image_reader.setFileName(wallpaper_filename) image_size = image_reader.size() image_size.scale(QSize(200, 125), Qt.IgnoreAspectRatio) image_reader.setScaledSize(image_size) thumbnail_image = image_reader.read() if thumbnail_image.isNull(): continue self.lw_wallpaper_history.add_item(thumbnail_image, wallpaper_date, wallpaper_copyright, archive_path=wallpaper_filename) self.app.processEvents() self.lw_wallpaper_history.sortItems(Qt.AscendingOrder)