def _load(filePath): # type: (pathlib.Path) -> NoReturn with itemsLock: icon = self.__iconsCache.get(filePath) if icon is None: iconProvider = QFileIconProvider() posixPath = filePath.as_posix() file = QFileInfo(posixPath) icon = iconProvider.icon(file) if icon.isNull(): mimeDb = QMimeDatabase() for mime in mimeDb.mimeTypesForFileName(posixPath): icon = QIcon.fromTheme(mime.iconName()) if not icon.isNull(): break result = QFileIconLoader.LoadResult(filePath, icon) with itemsLock: loadedItems[filePath] = result self.__iconsCache.set(filePath, icon) self.loaded.emit(result) if len(loadedItems) == len(targetPaths): self.completed.emit(loadedItems)
def load_image(parent, filename=None): nothing = [None] * 3 settings = QSettings() if filename is None: dialog = QFileDialog(parent, parent.tr('Load image'), settings.value('load_folder')) dialog.setOption(QFileDialog.DontUseNativeDialog, True) dialog.setFileMode(QFileDialog.ExistingFile) dialog.setViewMode(QFileDialog.Detail) mime_filters = [ "image/jpeg", "image/png", "image/tiff", "image/gif", "image/bmp", "image/webp", "image/x-portable-pixmap", "image/x-portable-graymap", "image/x-portable-bitmap" ] mime_db = QMimeDatabase() mime_patterns = [ mime_db.mimeTypeForName(mime).globPatterns() for mime in mime_filters ] all_formats = 'Supported formats ({})'.format(' '.join( [item for sub in mime_patterns for item in sub])) dialog.setMimeTypeFilters(mime_filters) name_filters = dialog.nameFilters() dialog.setNameFilters(name_filters + [all_formats]) dialog.selectNameFilter(all_formats) if dialog.exec_(): filename = dialog.selectedFiles()[0] else: return nothing if filename.endswith('.gif'): capture = cv.VideoCapture(filename) frames = int(capture.get(cv.CAP_PROP_FRAME_COUNT)) if frames > 1: QMessageBox.warning( parent, parent.tr('Warning'), parent.tr('Animated GIF: importing first frame')) result, image = capture.read() if not result: QMessageBox.critical(parent, parent.tr('Error'), parent.tr('Unable to decode GIF!')) return nothing if len(image.shape) == 2: image = cv.cvtColor(image, cv.COLOR_GRAY2BGR) else: image = cv.imread(filename, cv.IMREAD_COLOR) if image is None: QMessageBox.critical(parent, parent.tr('Error'), parent.tr('Unable to load image!')) return nothing if image.shape[2] > 3: QMessageBox.warning(parent, parent.tr('Warning'), parent.tr('Alpha channel discarded')) image = cv.cvtColor(image, cv.COLOR_BGRA2BGR) settings.setValue('load_folder', QFileInfo(filename).absolutePath()) return filename, os.path.basename(filename), image
def load_image(parent, filename=None): nothing = [None] * 3 settings = QSettings() mime_filters = ['image/jpeg', 'image/png', 'image/tiff', 'image/gif', 'image/bmp', 'image/webp', 'image/x-portable-pixmap', 'image/x-portable-graymap', 'image/x-portable-bitmap', 'image/x-nikon-nef', 'image/x-fuji-raf', 'image/x-canon-cr2', 'image/x-adobe-dng', 'image/x-sony-arw', 'image/x-kodak-dcr', 'image/x-minolta-mrw', 'image/x-pentax-pef', 'image/x-canon-crw', 'image/x-sony-sr2', 'image/x-olympus-orf', 'image/x-panasonic-raw'] mime_db = QMimeDatabase() mime_patterns = [mime_db.mimeTypeForName(mime).globPatterns() for mime in mime_filters] all_formats = 'Supported formats ({})'.format(' '.join([item for sub in mime_patterns for item in sub])) raw_exts = [p[0][-3:] for p in mime_patterns][-12:] if filename is None: dialog = QFileDialog(parent, parent.tr('Load image'), settings.value('load_folder')) dialog.setOption(QFileDialog.DontUseNativeDialog, True) dialog.setFileMode(QFileDialog.ExistingFile) dialog.setViewMode(QFileDialog.Detail) dialog.setMimeTypeFilters(mime_filters) name_filters = dialog.nameFilters() dialog.setNameFilters(name_filters + [all_formats]) dialog.selectNameFilter(all_formats) if dialog.exec_(): filename = dialog.selectedFiles()[0] else: return nothing ext = os.path.splitext(filename)[1][1:].lower() if ext in raw_exts: with rawpy.imread(filename) as raw: image = cv.cvtColor(raw.postprocess(use_auto_wb=True), cv.COLOR_RGB2BGR) elif ext == 'gif': capture = cv.VideoCapture(filename) frames = int(capture.get(cv.CAP_PROP_FRAME_COUNT)) if frames > 1: QMessageBox.warning(parent, parent.tr('Warning'), parent.tr('Animated GIF: importing first frame')) result, image = capture.read() if not result: QMessageBox.critical(parent, parent.tr('Error'), parent.tr('Unable to decode GIF!')) return nothing if len(image.shape) == 2: image = cv.cvtColor(image, cv.COLOR_GRAY2BGR) else: image = cv.imread(filename, cv.IMREAD_COLOR) if image is None: QMessageBox.critical(parent, parent.tr('Error'), parent.tr('Unable to load image!')) return nothing if image.shape[2] > 3: QMessageBox.warning(parent, parent.tr('Warning'), parent.tr('Alpha channel discarded')) image = cv.cvtColor(image, cv.COLOR_BGRA2BGR) settings.setValue('load_folder', QFileInfo(filename).absolutePath()) return filename, os.path.basename(filename), image
def testMimeTypeForName(self): db = QMimeDatabase() s0 = db.mimeTypeForName("application/x-zerosize") self.assertTrue(s0.isValid()) self.assertEqual(s0.name(), "application/x-zerosize") if "en" in QLocale().name(): self.assertEqual(s0.comment(), "empty document") s0Again = db.mimeTypeForName("application/x-zerosize") self.assertEqual(s0Again.name(), s0.name()) s1 = db.mimeTypeForName("text/plain") self.assertTrue(s1.isValid()) self.assertEqual(s1.name(), "text/plain") krita = db.mimeTypeForName("application/x-krita") self.assertTrue(krita.isValid()) rdf = db.mimeTypeForName("application/rdf+xml") self.assertTrue(rdf.isValid()) self.assertEqual(rdf.name(), "application/rdf+xml") if "en" in QLocale().name(): self.assertEqual(rdf.comment(), "RDF file") bzip2 = db.mimeTypeForName("application/x-bzip2") self.assertTrue(bzip2.isValid()) if "en" in QLocale().name(): self.assertEqual(bzip2.comment(), "Bzip archive") defaultMime = db.mimeTypeForName("application/octet-stream") self.assertTrue(defaultMime.isValid()) self.assertEqual(defaultMime.name(), "application/octet-stream") self.assertTrue(defaultMime.isDefault()) doesNotExist = db.mimeTypeForName("foobar/x-doesnot-exist") self.assertTrue(not doesNotExist.isValid())
def testMimeTypeForName(self): db = QMimeDatabase() s0 = db.mimeTypeForName("application/x-zerosize") self.assertTrue(s0.isValid()) self.assertEqual(s0.name(), "application/x-zerosize") self.assertEqual(s0.comment(), "empty document") s0Again = db.mimeTypeForName("application/x-zerosize") self.assertEqual(s0Again.name(), s0.name()) s1 = db.mimeTypeForName("text/plain") self.assertTrue(s1.isValid()) self.assertEqual(s1.name(), "text/plain") # print("Comment is %s" % s1.comment()) krita = db.mimeTypeForName("application/x-krita") self.assertTrue(krita.isValid()) # Test <comment> parsing with application/rdf+xml which has the english comment after the other ones rdf = db.mimeTypeForName("application/rdf+xml") self.assertTrue(rdf.isValid()) self.assertEqual(rdf.name(), "application/rdf+xml") self.assertEqual(rdf.comment(), "RDF file") bzip2 = db.mimeTypeForName("application/x-bzip2") self.assertTrue(bzip2.isValid()) self.assertEqual(bzip2.comment(), "Bzip archive") defaultMime = db.mimeTypeForName("application/octet-stream") self.assertTrue(defaultMime.isValid()) self.assertEqual(defaultMime.name(), "application/octet-stream") self.assertTrue(defaultMime.isDefault()) doesNotExist = db.mimeTypeForName("foobar/x-doesnot-exist") self.assertTrue(not doesNotExist.isValid())