def show_album(self, album): if album: self.albums.setCurrentIndex(self.albums.findData(album.id.text)) self.widgets['description'].set_value(album.summary.text) self.widgets['location'].setText(album.location.text) self.widgets['access'].setCurrentIndex( self.widgets['access'].findData(album.access.text)) self.widgets['timestamp'].setDateTime( QtCore.QDateTime.fromTime_t(int(album.timestamp.text) // 1000)) if album.group.thumbnail is not None: QtWidgets.QApplication.processEvents() with Busy(): url = album.group.thumbnail[0].get('url') image = QtGui.QPixmap(160, 160) try: image.loadFromData(urlopen(url).read()) except HTTPError as ex: paint = QtGui.QPainter(image) paint.fillRect(image.rect(), Qt.black) paint.setPen(Qt.white) paint.drawText(image.rect(), Qt.AlignLeft | Qt.TextWordWrap, str(ex)) paint.end() self.album_thumb.setPixmap(image) else: self.widgets['description'].set_value(None) self.widgets['location'].clear() self.widgets['timestamp'].clear() self.album_thumb.clear()
def mouseMoveEvent(self, event): if not self.image_list.drag_icon: return if qt_version_info >= (6, 0): pos = event.position() else: pos = event.pos() if ((pos - self.drag_start_pos).manhattanLength() < QtWidgets.QApplication.startDragDistance()): return if not self.get_selected(): # user has started dragging an unselected image self.image_list.select_image(self) paths = [] for image in self.image_list.get_selected_images(): paths.append(image.path) if not paths: return drag = QtGui.QDrag(self) # construct icon count = min(len(paths), 8) src_icon = self.image_list.drag_icon src_w = src_icon.width() src_h = src_icon.height() margin = (count - 1) * 4 if count == 1: icon = src_icon else: icon = QtGui.QPixmap(src_w + margin, src_h + margin) icon.fill(Qt.transparent) try: paint = QtGui.QPainter(icon) for i in range(count): paint.drawPixmap(QtCore.QPoint(margin - (i * 4), i * 4), src_icon) finally: del paint drag.setPixmap(icon) if self.image_list.drag_hotspot: x, y = self.image_list.drag_hotspot else: x, y = src_w // 2, src_h drag.setHotSpot(QtCore.QPoint(x, y + margin)) mimeData = QtCore.QMimeData() mimeData.setData(DRAG_MIMETYPE, repr(paths).encode('utf-8')) drag.setMimeData(mimeData) if qt_version_info >= (6, 0): drag.exec(Qt.CopyAction) else: drag.exec_(Qt.CopyAction)
def __init__(self, *arg, **kw): self.upload_config = FacebookUploadConfig() super(FacebookUploader, self).__init__(self.upload_config, *arg, **kw) self.upload_config.new_album.connect(self.new_album) self.upload_config.select_album.connect(self.select_album) self.service_name = self.tr('Facebook') self.image_types = { 'accepted': ('image/jpeg', 'image/png'), 'rejected': ('image/x-portable-anymap', 'image/x-dcraw'), } self.login_popup = None # add Facebook icon to connect button icon_file = pkg_resources.resource_filename('photini', 'data/facebook_logo.png') self.user_connect.setIcon(QtGui.QIcon(QtGui.QPixmap(icon_file)))
def get_album_thumb(self, album): if album.group.thumbnail is None: return None image = QtGui.QPixmap(160, 160) try: resp = self._check_response( self.session.get(album.group.thumbnail[0].get('url'))) image.loadFromData(resp.content) except Exception as ex: paint = QtGui.QPainter(image) paint.fillRect(image.rect(), Qt.black) paint.setPen(Qt.white) paint.drawText(image.rect(), Qt.AlignLeft | Qt.TextWordWrap, str(ex)) paint.end() return image
def transform(self, pixmap, orientation): orientation = (orientation or 1) - 1 if not orientation: return pixmap # need to rotate and or reflect image transform = QtGui.QTransform() if orientation & 0b001: # reflect left-right transform = transform.scale(-1.0, 1.0) if orientation & 0b010: transform = transform.rotate(180.0) if orientation & 0b100: # transpose horizontal & vertical transform = QtGui.QTransform(0, 1, 1, 0, 1, 1) * transform return pixmap.transformed(transform)
def get_qt_image(self): reader = QtGui.QImageReader(self.path) reader.setAutoTransform(False) qt_im = reader.read() if not qt_im or qt_im.isNull(): logger.error('Image read: %s: %s', self.path, reader.errorString()) return None w = qt_im.width() h = qt_im.height() if max(w, h) > 1000: # use Qt's scaling (not high quality) to pre-shrink large # images qt_im = qt_im.scaled(1000, 1000, Qt.KeepAspectRatio, Qt.SmoothTransformation) w = qt_im.width() h = qt_im.height() # pad image to 4:3 (or 3:4) aspect ratio if w >= h: new_h = int(0.5 + (float(w * 3) / 4.0)) new_w = int(0.5 + (float(h * 4) / 3.0)) else: new_h = int(0.5 + (float(w * 4) / 3.0)) new_w = int(0.5 + (float(h * 3) / 4.0)) if new_w > w: pad = (new_w - w) // 2 qt_im = qt_im.copy(-pad, 0, new_w, h) elif new_h > h: pad = (new_h - h) // 2 qt_im = qt_im.copy(0, -pad, w, new_h) return qt_im
def convert(value): if value['data'] and not value['image']: buf = QtCore.QBuffer() buf.setData(value['data']) reader = QtGui.QImageReader(buf) reader.setAutoTransform(False) value['fmt'] = reader.format().data().decode().upper() value['image'] = reader.read() if value['image'].isNull(): logger.error('thumbnail: %s', reader.errorString()) value['image'] = None # don't keep reference to what might be an entire image file value['data'] = None if value['image'] and not value['data']: buf = QtCore.QBuffer() buf.open(buf.WriteOnly) value['fmt'] = 'JPEG' value['image'].save(buf, value['fmt']) value['data'] = buf.data().data() if value['image']: value['w'] = value['image'].width() value['h'] = value['image'].height() else: value['w'] = 0 value['h'] = 0 return value
def __init__(self, *arg, **kw): super(BingMap, self).__init__(*arg, **kw) if self.app.test_mode: self.drag_icon = QtGui.QPixmap( os.path.join(self.script_dir, 'grey_marker_v8.png')) self.map.settings().setAttribute( QtWebKit.QWebSettings.LocalContentCanAccessRemoteUrls, True) self.map.settings().setAttribute( QtWebKit.QWebSettings.LocalContentCanAccessFileUrls, True)
def highlightBlock(self, text): if not (self.spell_check.enabled and self.spell_check.dict): return formatter = QtGui.QTextCharFormat() formatter.setUnderlineColor(Qt.red) formatter.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline) for word in self.words.finditer(text): if not self.spell_check.dict.check(word.group()): self.setFormat(word.start(), word.end() - word.start(), formatter)
def initialize_finished(self): QtWidgets.QApplication.restoreOverrideCursor() self.map_loaded = True self.edit_box.setEnabled(True) self.map.setAcceptDrops(True) drag_icon = QtGui.QPixmap( os.path.join(self.script_dir, '../map_pin_grey.png')) drag_hotspot = 11, 35 self.image_list.set_drag_to_map(drag_icon, drag_hotspot) self.redraw_markers() self.display_coords()
def load_thumbnail(self): pixmap = QtGui.QPixmap() thumb = self.metadata.thumbnail if thumb: pixmap.loadFromData(thumb.data) if pixmap.isNull(): self.image.setText(self.tr('No\nthumbnail\nin file')) return pixmap = self.transform(pixmap, self.metadata.orientation) self.image.setPixmap( pixmap.scaled(self.thumb_size, self.thumb_size, Qt.KeepAspectRatio, Qt.SmoothTransformation))
def show_user(self, name, picture): if name: self.user_name.setText( self.tr('Connected to {0} on {1}').format( name, self.service_name)) else: self.user_name.setText( self.tr('Not connected to {}').format(self.service_name)) pixmap = QtGui.QPixmap() if picture: pixmap.loadFromData(picture) self.user_photo.setPixmap(pixmap)
def show_user(self, name, picture): if name: self.user_name.setText(translate( 'UploaderTabsAll', 'Logged in as {0} on {1}').format(name, self.service_name)) else: self.user_name.setText(translate( 'UploaderTabsAll', 'Not logged in to {}').format(self.service_name)) pixmap = QtGui.QPixmap() if picture: pixmap.loadFromData(picture) self.user_photo.setPixmap(pixmap)
def __init__(self, *args, **kw): super(LocationInfo, self).__init__(*args, **kw) layout = QtWidgets.QGridLayout() self.setLayout(layout) layout.setContentsMargins(0, 0, 0, 0) self.members = { 'taken': LocationWidgets(self), 'shown': LocationWidgets(self) } self.swap = SquareButton(six.chr(0x21c4)) self.swap.setStyleSheet('QPushButton { font-size: 10px }') self.swap.setFont(QtGui.QFont("Dejavu Sans")) if not self.swap.fontInfo().exactMatch(): # probably on Windows, try a different font self.swap.setFont(QtGui.QFont("Segoe UI Symbol")) layout.addWidget(self.swap, 0, 4) label = QtWidgets.QLabel(translate('PhotiniMap', 'camera')) layout.addWidget(label, 0, 1, 1, 2) label = QtWidgets.QLabel(translate('PhotiniMap', 'subject')) layout.addWidget(label, 0, 3) layout.addWidget(QtWidgets.QLabel(translate('PhotiniMap', 'Street:')), 1, 0) layout.addWidget(QtWidgets.QLabel(translate('PhotiniMap', 'City:')), 2, 0) layout.addWidget( QtWidgets.QLabel(translate('PhotiniMap', 'Province:')), 3, 0) layout.addWidget(QtWidgets.QLabel(translate('PhotiniMap', 'Country:')), 4, 0) layout.addWidget(QtWidgets.QLabel(translate('PhotiniMap', 'Region:')), 5, 0) for ts, col in (('taken', 1), ('shown', 3)): layout.addWidget(self.members[ts]['sublocation'], 1, col, 1, 2) layout.addWidget(self.members[ts]['city'], 2, col, 1, 2) layout.addWidget(self.members[ts]['province_state'], 3, col, 1, 2) layout.addWidget(self.members[ts]['country_name'], 4, col) layout.addWidget(self.members[ts]['country_code'], 4, col + 1) layout.addWidget(self.members[ts]['world_region'], 5, col, 1, 2)
def mouseMoveEvent(self, event): if not self.image_list.drag_icon: return if ((event.pos() - self.drag_start_pos).manhattanLength() < QtWidgets.QApplication.startDragDistance()): return paths = [] for image in self.image_list.get_selected_images(): paths.append(image.path) if not paths: return drag = QtGui.QDrag(self) # construct icon count = min(len(paths), 8) src_icon = self.image_list.drag_icon src_w = src_icon.width() src_h = src_icon.height() margin = (count - 1) * 4 if count == 1: icon = src_icon else: icon = QtGui.QPixmap(src_w + margin, src_h + margin) icon.fill(Qt.transparent) with QtGui.QPainter(icon) as paint: for i in range(count): paint.drawPixmap( QtCore.QPoint(margin - (i * 4), i * 4), src_icon) drag.setPixmap(icon) if src_h == src_w: # round marker used in Bing maps version 8 drag.setHotSpot(QtCore.QPoint(src_w // 2, (src_h // 2) + margin)) else: drag.setHotSpot(QtCore.QPoint(src_w // 2, src_h + margin)) mimeData = QtCore.QMimeData() mimeData.setData(DRAG_MIMETYPE, repr(paths).encode('utf-8')) drag.setMimeData(mimeData) dropAction = drag.exec_(Qt.CopyAction)
def show_user(self, name, picture): if name: self.user_name.setText( self.tr('Connected to {0} on {1}').format( name, self.service_name)) else: self.user_name.setText( self.tr('Not connected to {}').format(self.service_name)) pixmap = QtGui.QPixmap() if picture: try: pixmap.loadFromData(urlopen(picture).read()) except URLError as ex: self.logger.error('cannot read %s: %s', picture, str(ex)) self.user_photo.setPixmap(pixmap)
def show_album(self, album, picture): if 'description' in album: self.widgets['album_description'].setPlainText( album['description']) else: self.widgets['album_description'].clear() if 'location' in album: self.widgets['album_location'].setText(album['location']) else: self.widgets['album_location'].clear() pixmap = QtGui.QPixmap() if picture: rsp = requests.get(picture) if rsp.status_code == 200: pixmap.loadFromData(rsp.content) else: logger.error('HTTP error %d (%s)', rsp.status_code, picture) self.widgets['album_thumb'].setPixmap(pixmap)
def show_album(self, album, picture): if 'description' in album: self.widgets['album_description'].setPlainText( album['description']) else: self.widgets['album_description'].clear() if 'location' in album: self.widgets['album_location'].setText(album['location']) else: self.widgets['album_location'].clear() pixmap = QtGui.QPixmap() if picture: try: rsp = requests.get(picture) pixmap.loadFromData(rsp.content) except Exception as ex: self.logger.error('cannot read %s: %s', picture, str(ex)) self.widgets['album_thumb'].setPixmap(pixmap)
def write(self, handler, tag): if handler.is_xmp_tag(tag): data = self.data if self.fmt != 'JPEG': pixmap = QtGui.QPixmap() pixmap.loadFromData(data) buf = QtCore.QBuffer() buf.open(QtCore.QIODevice.WriteOnly) pixmap.save(buf, 'JPEG') data = buf.data().data() w = pixmap.width() h = pixmap.height() else: w, h = self.size() data = codecs.encode(data, 'base64_codec') if not six.PY2: data = data.decode('ascii') handler.set_string(tag, (data, 'JPEG', str(w), str(h))) elif handler.is_exif_tag(tag): handler.set_exif_thumbnail_from_buffer(self.data)
def get_video_frame(self): if not cv2: return video = cv2.VideoCapture(self.path) if not video.isOpened(): return OK, cv_image = video.read() if not OK: return height, width, channel = cv_image.shape fmt = QtGui.QImage.Format_RGB888 # need to pad to 4 pixel multiple new_width = width - (width % -4) if channel == 4: # assume BGRA fmt = QtGui.QImage.Format_ARGB32 np_image = np.empty((height, new_width, channel), dtype=np.uint8) np_image[:, :width, 0] = cv_image[:, :, 3] np_image[:, :width, 1] = cv_image[:, :, 2] np_image[:, :width, 2] = cv_image[:, :, 1] np_image[:, :width, 3] = cv_image[:, :, 0] elif channel == 3: # assume BGR np_image = np.empty((height, new_width, channel), dtype=np.uint8) np_image[:, :width, 0] = cv_image[:, :, 2] np_image[:, :width, 1] = cv_image[:, :, 1] np_image[:, :width, 2] = cv_image[:, :, 0] elif channel == 1: # assume Y channel = 3 np_image = np.empty((height, new_width, channel), dtype=np.uint8) np_image[:, :width, 0] = cv_image[:, :, 0] np_image[:, :width, 1] = cv_image[:, :, 0] np_image[:, :width, 2] = cv_image[:, :, 0] else: return bpl = new_width * channel qt_im = QtGui.QImage(np_image.data, width, height, bpl, fmt) # attach np_image so it isn't deleted until qt_im is qt_im._data = np_image return qt_im
def load_thumbnail(self): if self.pixmap.isNull(): self.image.setText(self.tr('Can not\nload\nimage')) else: pixmap = self.pixmap orientation = self.metadata.orientation if orientation and orientation.value > 1: # need to rotate and or reflect image transform = QtGui.QTransform() if orientation.value in (3, 4): transform = transform.rotate(180.0) elif orientation.value in (5, 6): transform = transform.rotate(90.0) elif orientation.value in (7, 8): transform = transform.rotate(-90.0) if orientation.value in (2, 4, 5, 7): transform = transform.scale(-1.0, 1.0) pixmap = pixmap.transformed(transform) self.image.setPixmap(pixmap.scaled( self.thumb_size, self.thumb_size, Qt.KeepAspectRatio, Qt.SmoothTransformation))
def get_qt_image(self): qt_im = QtGui.QImage(self.path) if not qt_im or qt_im.isNull(): logger.error('Cannot read %s image data from %s', self.file_type, self.path) return None # reorient if required if self.file_type in ('image/x-canon-cr2', 'image/x-nikon-nef'): qt_im = self.transform(qt_im, self.metadata.orientation, inverse=True) w = qt_im.width() h = qt_im.height() if max(w, h) > 6000: # use Qt's scaling (not high quality) to pre-shrink very # large images, to avoid PIL "DecompressionBombWarning" return qt_im.scaled(6000, 6000, Qt.KeepAspectRatio, Qt.SmoothTransformation) w = qt_im.width() h = qt_im.height() # pad image to 4:3 (or 3:4) aspect ratio if w >= h: new_h = int(0.5 + (float(w * 3) / 4.0)) new_w = int(0.5 + (float(h * 4) / 3.0)) if new_h > h: pad = (new_h - h) // 2 qt_im = qt_im.copy(0, -pad, w, new_h) elif new_w > w: pad = (new_w - w) // 2 qt_im = qt_im.copy(-pad, 0, new_w, h) else: new_h = int(0.5 + (float(w * 4) / 3.0)) new_w = int(0.5 + (float(h * 3) / 4.0)) if new_w > w: pad = (new_w - w) // 2 qt_im = qt_im.copy(-pad, 0, new_w, h) elif new_h > h: pad = (new_h - h) // 2 qt_im = qt_im.copy(0, -pad, w, new_h) return qt_im
def convert(value): if value['data'] and not value['image']: buf = QtCore.QBuffer() buf.setData(value['data']) reader = QtGui.QImageReader(buf) reader.setAutoTransform(False) value['fmt'] = reader.format().data().decode().upper() value['image'] = reader.read() if value['image'].isNull(): logger.error('thumbnail: %s', reader.errorString()) value['image'] = None if value['image']: value['w'] = value['image'].width() value['h'] = value['image'].height() if value['data'] and len(value['data']) >= 60000: # don't keep unusably large amount of data value['data'] = None else: value['w'] = 0 value['h'] = 0 value['data'] = None return value
def __init__(self, options, initial_files): super(MainWindow, self).__init__() self.setWindowTitle(self.tr("Photini photo metadata editor")) pixmap = QtGui.QPixmap() pixmap.loadFromData( pkg_resources.resource_string('photini', 'data/icons/48/photini.png')) icon = QtGui.QIcon(pixmap) self.setWindowIcon(icon) self.selection = list() # logger window self.loggerwindow = LoggerWindow(options.verbose) self.loggerwindow.setWindowIcon(icon) self.logger = logging.getLogger(self.__class__.__name__) # set network proxy proxies = getproxies() if 'http' in proxies: parsed = urlparse(proxies['http']) QNetworkProxy.setApplicationProxy( QNetworkProxy(QNetworkProxy.HttpProxy, parsed.hostname, parsed.port)) # create shared global objects self.app = QtWidgets.QApplication.instance() self.app.config_store = ConfigStore('editor', parent=self) self.app.spell_check = SpellCheck(parent=self) self.app.test_mode = options.test # set debug mode if self.app.test_mode: debug_metadata() # restore size size = self.width(), self.height() self.resize( *eval(self.app.config_store.get('main_window', 'size', str(size)))) # image selector self.image_list = ImageList() self.image_list.selection_changed.connect(self.new_selection) self.image_list.new_metadata.connect(self.new_metadata) # prepare list of tabs and associated stuff self.tab_list = ( { 'name': self.tr('&Descriptive metadata'), 'key': 'descriptive_metadata', 'class': Descriptive }, { 'name': self.tr('&Technical metadata'), 'key': 'technical_metadata', 'class': Technical }, { 'name': self.tr('Map (&Google)'), 'key': 'map_google', 'class': GoogleMap }, { 'name': self.tr('Map (&Bing)'), 'key': 'map_bing', 'class': BingMap }, { 'name': self.tr('Map (&OSM)'), 'key': 'map_osm', 'class': OpenStreetMap }, { 'name': self.tr('&Flickr upload'), 'key': 'flickr_upload', 'class': FlickrUploader }, { 'name': self.tr('Google &Photos upload'), 'key': 'picasa_upload', 'class': PicasaUploader }, { 'name': self.tr('Faceboo&k upload'), 'key': 'facebook_upload', 'class': FacebookUploader }, { 'name': self.tr('&Import photos'), 'key': 'import_photos', 'class': Importer }, ) # file menu file_menu = self.menuBar().addMenu(self.tr('File')) open_action = QtWidgets.QAction(self.tr('Open images'), self) open_action.setShortcuts(QtGui.QKeySequence.Open) open_action.triggered.connect(self.image_list.open_files) file_menu.addAction(open_action) self.save_action = QtWidgets.QAction( self.tr('Save images with new data'), self) self.save_action.setShortcuts(QtGui.QKeySequence.Save) self.save_action.setEnabled(False) self.save_action.triggered.connect(self.image_list.save_files) file_menu.addAction(self.save_action) self.close_action = QtWidgets.QAction(self.tr('Close selected images'), self) self.close_action.setEnabled(False) self.close_action.triggered.connect(self.close_files) file_menu.addAction(self.close_action) close_all_action = QtWidgets.QAction(self.tr('Close all images'), self) close_all_action.triggered.connect(self.close_all_files) file_menu.addAction(close_all_action) file_menu.addSeparator() quit_action = QtWidgets.QAction(self.tr('Quit'), self) quit_action.setShortcuts( [QtGui.QKeySequence.Quit, QtGui.QKeySequence.Close]) quit_action.triggered.connect( QtWidgets.QApplication.instance().closeAllWindows) file_menu.addAction(quit_action) # options menu options_menu = self.menuBar().addMenu(self.tr('Options')) settings_action = QtWidgets.QAction(self.tr('Settings'), self) settings_action.triggered.connect(self.edit_settings) options_menu.addAction(settings_action) options_menu.addSeparator() for tab in self.tab_list: name = tab['name'].replace('&', '') tab['action'] = QtWidgets.QAction(name, self) tab['action'].setCheckable(True) if tab['class']: tab['action'].setChecked( eval(self.app.config_store.get('tabs', tab['key'], 'True'))) else: tab['action'].setEnabled(False) tab['action'].triggered.connect(self.add_tabs) options_menu.addAction(tab['action']) # spelling menu languages = self.app.spell_check.available_languages() spelling_menu = self.menuBar().addMenu(self.tr('Spelling')) enable_action = QtWidgets.QAction(self.tr('Enable spell check'), self) enable_action.setEnabled(bool(languages)) enable_action.setCheckable(True) enable_action.setChecked(self.app.spell_check.enabled) enable_action.toggled.connect(self.app.spell_check.enable) spelling_menu.addAction(enable_action) language_menu = QtWidgets.QMenu(self.tr('Choose language'), self) language_menu.setEnabled(bool(languages)) language_group = QtWidgets.QActionGroup(self) current_language = self.app.spell_check.current_language() for tag in languages: language_action = QtWidgets.QAction(tag, self) language_action.setCheckable(True) language_action.setChecked(tag == current_language) language_action.setActionGroup(language_group) language_menu.addAction(language_action) language_group.triggered.connect(self.app.spell_check.set_language) spelling_menu.addMenu(language_menu) # help menu help_menu = self.menuBar().addMenu(self.tr('Help')) about_action = QtWidgets.QAction(self.tr('About Photini'), self) about_action.triggered.connect(self.about) help_menu.addAction(about_action) help_menu.addSeparator() help_action = QtWidgets.QAction(self.tr('Photini documentation'), self) help_action.triggered.connect(self.open_docs) help_menu.addAction(help_action) # main application area self.central_widget = QtWidgets.QSplitter() self.central_widget.setOrientation(Qt.Vertical) self.central_widget.setChildrenCollapsible(False) self.tabs = QtWidgets.QTabWidget() self.tabs.setTabBar(QTabBar()) self.tabs.setElideMode(Qt.ElideRight) self.tabs.currentChanged.connect(self.new_tab) self.add_tabs() self.central_widget.addWidget(self.tabs) self.central_widget.addWidget(self.image_list) size = self.central_widget.sizes() self.central_widget.setSizes( eval(self.app.config_store.get('main_window', 'split', str(size)))) self.central_widget.splitterMoved.connect(self.new_split) self.setCentralWidget(self.central_widget) # open files given on command line, after GUI is displayed self.initial_files = initial_files if self.initial_files: QtCore.QTimer.singleShot(0, self.open_initial_files)
def __init__(self, path, image_list, thumb_size=80, *arg, **kw): super(Image, self).__init__(*arg, **kw) self.path = path self.image_list = image_list self.name, ext = os.path.splitext(os.path.basename(self.path)) self.selected = False self.thumb_size = thumb_size # read image with open(self.path, 'rb') as pf: image_data = pf.read() # read metadata self.metadata = Metadata( self.path, image_data, new_status=self.show_status) # set file type ext = ext.lower() self.file_type = imghdr.what(self.path) or 'raw' if self.file_type == 'tiff' and ext not in ('.tif', '.tiff'): self.file_type = 'raw' # make 'master' thumbnail self.pixmap = QtGui.QPixmap() self.pixmap.loadFromData(image_data) unrotate = self.file_type == 'raw' if self.pixmap.isNull(): # image read failed so attempt to use exif thumbnail thumb = self.metadata.get_exif_thumbnail() if thumb: self.pixmap.loadFromData(bytearray(thumb)) unrotate = False if not self.pixmap.isNull(): if max(self.pixmap.width(), self.pixmap.height()) > 450: # store a scaled down version of image to save memory self.pixmap = self.pixmap.scaled( 300, 300, Qt.KeepAspectRatio, Qt.SmoothTransformation) if unrotate: # loading preview which is already re-oriented orientation = self.metadata.orientation if orientation and orientation.value > 1: # need to unrotate and or unreflect image transform = QtGui.QTransform() if orientation.value in (3, 4): transform = transform.rotate(180.0) elif orientation.value in (5, 6): transform = transform.rotate(-90.0) elif orientation.value in (7, 8): transform = transform.rotate(90.0) if orientation.value in (2, 4, 5, 7): transform = transform.scale(-1.0, 1.0) self.pixmap = self.pixmap.transformed(transform) # sub widgets layout = QtWidgets.QGridLayout() layout.setSpacing(0) layout.setContentsMargins(3, 3, 3, 3) self.setLayout(layout) self.setToolTip(self.path) # label to display image self.image = QtWidgets.QLabel() self.image.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) layout.addWidget(self.image, 0, 0, 1, 2) # label to display file name self.label = QtWidgets.QLabel() self.label.setAlignment(Qt.AlignRight) self.label.setStyleSheet("QLabel { font-size: 12px }") layout.addWidget(self.label, 1, 1) # label to display status self.status = QtWidgets.QLabel() self.status.setAlignment(Qt.AlignLeft) self.status.setStyleSheet("QLabel { font-size: 12px }") self.status.setFont(QtGui.QFont("Dejavu Sans")) if not self.status.fontInfo().exactMatch(): # probably on Windows, try a different font self.status.setFont(QtGui.QFont("Segoe UI Symbol")) layout.addWidget(self.status, 1, 0) self.setFrameStyle(QtWidgets.QFrame.Panel | QtWidgets.QFrame.Plain) self.setObjectName("thumbnail") self.set_selected(False) self.show_status(False) self._set_thumb_size(self.thumb_size)
def _find_local(self, photo, unknowns): granularity = int(photo['datetakengranularity']) min_taken_date = datetime.strptime( photo['datetaken'], '%Y-%m-%d %H:%M:%S') if granularity <= 0: max_taken_date = min_taken_date + timedelta(seconds=1) elif granularity <= 4: max_taken_date = min_taken_date + timedelta(days=31) else: max_taken_date = min_taken_date + timedelta(days=366) candidates = [] for candidate in unknowns: if not candidate.metadata.date_taken: continue date_taken = candidate.metadata.date_taken['datetime'] if date_taken < min_taken_date or date_taken > max_taken_date: continue candidates.append(candidate) if not candidates: return None rsp = requests.get(photo['url_t']) if rsp.status_code == 200: flickr_icon = rsp.content else: logger.error('HTTP error %d (%s)', rsp.status_code, photo['url_t']) return None # get user to choose matching image file dialog = QtWidgets.QDialog(parent=self) dialog.setWindowTitle(translate('FlickrTab', 'Select an image')) dialog.setLayout(QtWidgets.QFormLayout()) dialog.layout().setFieldGrowthPolicy( QtWidgets.QFormLayout.AllNonFixedFieldsGrow) pixmap = QtGui.QPixmap() pixmap.loadFromData(flickr_icon) label = QtWidgets.QLabel() label.setPixmap(pixmap) dialog.layout().addRow(label, QtWidgets.QLabel(translate( 'FlickrTab', 'Which image file matches\nthis picture on Flickr?'))) divider = QtWidgets.QFrame() divider.setFrameStyle(QtWidgets.QFrame.HLine) dialog.layout().addRow(divider) buttons = {} for candidate in candidates: label = QtWidgets.QLabel() pixmap = candidate.image.pixmap() if pixmap: label.setPixmap(pixmap) else: label.setText(candidate.image.text()) button = QtWidgets.QPushButton( os.path.basename(candidate.path)) button.setToolTip(candidate.path) button.setCheckable(True) button.clicked.connect(dialog.accept) dialog.layout().addRow(label, button) buttons[button] = candidate button = QtWidgets.QPushButton(translate('FlickrTab', 'No match')) button.setDefault(True) button.clicked.connect(dialog.reject) dialog.layout().addRow('', button) if dialog.exec_() == QtWidgets.QDialog.Accepted: for button, candidate in buttons.items(): if button.isChecked(): return candidate return None
def __init__(self, images, *arg, **kw): super(NewLensDialog, self).__init__(*arg, **kw) self.setWindowTitle(self.tr('Photini: define lens')) self.setLayout(QtWidgets.QVBoxLayout()) # main dialog area scroll_area = QtWidgets.QScrollArea() scroll_area.setWidgetResizable(True) self.layout().addWidget(scroll_area) panel = QtWidgets.QWidget() panel.setLayout(QtWidgets.QFormLayout()) # ok & cancel buttons button_box = QtWidgets.QDialogButtonBox( QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) button_box.accepted.connect(self.accept) button_box.rejected.connect(self.reject) self.layout().addWidget(button_box) # model self.lens_model = QtWidgets.QLineEdit() self.lens_model.setMinimumWidth(250) panel.layout().addRow(self.tr('Model name'), self.lens_model) # maker self.lens_make = QtWidgets.QLineEdit() panel.layout().addRow(self.tr("Maker's name"), self.lens_make) # serial number self.lens_serial = QtWidgets.QLineEdit() panel.layout().addRow(self.tr('Serial number'), self.lens_serial) ## spec has four items self.lens_spec = {} # min focal length self.lens_spec['min_fl'] = QtWidgets.QLineEdit() self.lens_spec['min_fl'].setValidator( QtGui.QDoubleValidator(bottom=0.0)) panel.layout().addRow(self.tr('Minimum focal length (mm)'), self.lens_spec['min_fl']) # min focal length aperture self.lens_spec['min_fl_fn'] = QtWidgets.QLineEdit() self.lens_spec['min_fl_fn'].setValidator(DoubleValidator(bottom=0.0)) panel.layout().addRow(self.tr('Aperture at min. focal length f/'), self.lens_spec['min_fl_fn']) # max focal length self.lens_spec['max_fl'] = QtWidgets.QLineEdit() self.lens_spec['max_fl'].setValidator( QtGui.QDoubleValidator(bottom=0.0)) panel.layout().addRow(self.tr('Maximum focal length (mm)'), self.lens_spec['max_fl']) # max focal length aperture self.lens_spec['max_fl_fn'] = QtWidgets.QLineEdit() self.lens_spec['max_fl_fn'].setValidator(DoubleValidator(bottom=0.0)) panel.layout().addRow(self.tr('Aperture at max. focal length f/'), self.lens_spec['max_fl_fn']) # add panel to scroll area after its size is known scroll_area.setWidget(panel) # fill in any values we can from existing metadata for image in images: if image.metadata.lens_model: self.lens_model.setText(image.metadata.lens_model.value) if image.metadata.lens_make: self.lens_make.setText(image.metadata.lens_make.value) if image.metadata.lens_serial: self.lens_serial.setText(image.metadata.lens_serial.value) spec = image.metadata.lens_spec for key in self.lens_spec: if spec and spec.value[key]: self.lens_spec[key].setText('{:g}'.format( float(spec.value[key])))
def __init__(self, options, initial_files): super(MainWindow, self).__init__() self.setWindowTitle(self.tr("Photini photo metadata editor")) pixmap = QtGui.QPixmap() pixmap.loadFromData(pkg_resources.resource_string( 'photini', 'data/icons/48/photini.png')) icon = QtGui.QIcon(pixmap) self.setWindowIcon(icon) self.selection = list() # logger window self.loggerwindow = LoggerWindow(options.verbose) self.loggerwindow.setWindowIcon(icon) # set network proxy proxies = getproxies() if 'http' in proxies: parsed = urlparse(proxies['http']) QNetworkProxy.setApplicationProxy(QNetworkProxy( QNetworkProxy.HttpProxy, parsed.hostname, parsed.port)) # create shared global objects self.app = QtWidgets.QApplication.instance() self.app.config_store = ConfigStore('editor', parent=self) self.app.spell_check = SpellCheck(parent=self) self.app.test_mode = options.test # restore size size = self.width(), self.height() self.resize(*eval( self.app.config_store.get('main_window', 'size', str(size)))) # image selector self.image_list = ImageList() self.image_list.selection_changed.connect(self.new_selection) self.image_list.new_metadata.connect(self.new_metadata) # update config file if self.app.config_store.config.has_section('tabs'): conv = { 'descriptive_metadata': 'photini.descriptive', 'technical_metadata' : 'photini.technical', 'map_google' : 'photini.googlemap', 'map_bing' : 'photini.bingmap', 'map_mapbox' : 'photini.mapboxmap', 'map_osm' : 'photini.openstreetmap', 'flickr_upload' : 'photini.flickr', 'import_photos' : 'photini.importer', } for key in self.app.config_store.config.options('tabs'): if key in conv: self.app.config_store.set( 'tabs', conv[key], self.app.config_store.get('tabs', key)) self.app.config_store.config.remove_option('tabs', key) # prepare list of tabs and associated stuff self.tab_list = [] modules = ('photini.descriptive', 'photini.technical', 'photini.googlemap', 'photini.bingmap', 'photini.mapboxmap', 'photini.openstreetmap', 'photini.flickr', 'photini.googlephotos', 'photini.importer') modules = eval(self.app.config_store.get( 'tabs', 'modules', pprint.pformat(modules))) for module in modules: tab = {'module': module} try: mod = importlib.import_module(tab['module']) tab['class'] = mod.TabWidget tab['name'] = tab['class'].tab_name() except ImportError as ex: print(str(ex)) tab['class'] = None self.tab_list.append(tab) # file menu file_menu = self.menuBar().addMenu(self.tr('File')) open_action = QtWidgets.QAction(self.tr('Open images'), self) open_action.setShortcuts(QtGui.QKeySequence.Open) open_action.triggered.connect(self.image_list.open_files) file_menu.addAction(open_action) self.save_action = QtWidgets.QAction( self.tr('Save images with new data'), self) self.save_action.setShortcuts(QtGui.QKeySequence.Save) self.save_action.setEnabled(False) self.save_action.triggered.connect(self.image_list.save_files) file_menu.addAction(self.save_action) self.close_action = QtWidgets.QAction( self.tr('Close selected images'), self) self.close_action.setEnabled(False) self.close_action.triggered.connect(self.close_files) file_menu.addAction(self.close_action) close_all_action = QtWidgets.QAction(self.tr('Close all images'), self) close_all_action.triggered.connect(self.close_all_files) file_menu.addAction(close_all_action) if GpxImporter: file_menu.addSeparator() self.import_gpx_action = QtWidgets.QAction( self.tr('Import GPX file'), self) self.import_gpx_action.triggered.connect(self.import_pgx_file) file_menu.addAction(self.import_gpx_action) else: self.import_gpx_action = None file_menu.addSeparator() quit_action = QtWidgets.QAction(self.tr('Quit'), self) quit_action.setShortcuts( [QtGui.QKeySequence.Quit, QtGui.QKeySequence.Close]) quit_action.triggered.connect( QtWidgets.QApplication.instance().closeAllWindows) file_menu.addAction(quit_action) # options menu options_menu = self.menuBar().addMenu(self.tr('Options')) settings_action = QtWidgets.QAction(self.tr('Settings'), self) settings_action.triggered.connect(self.edit_settings) options_menu.addAction(settings_action) options_menu.addSeparator() for tab in self.tab_list: if tab['class']: name = tab['name'].replace('&', '') else: name = tab['module'] tab['action'] = QtWidgets.QAction(name, self) tab['action'].setCheckable(True) if tab['class']: tab['action'].setChecked(eval( self.app.config_store.get('tabs', tab['module'], 'True'))) else: tab['action'].setEnabled(False) tab['action'].triggered.connect(self.add_tabs) options_menu.addAction(tab['action']) # spelling menu languages = self.app.spell_check.available_languages() spelling_menu = self.menuBar().addMenu(self.tr('Spelling')) enable_action = QtWidgets.QAction(self.tr('Enable spell check'), self) enable_action.setEnabled(languages is not None) enable_action.setCheckable(True) enable_action.setChecked(self.app.spell_check.enabled) enable_action.toggled.connect(self.app.spell_check.enable) spelling_menu.addAction(enable_action) language_menu = QtWidgets.QMenu(self.tr('Choose language'), self) language_menu.setEnabled(languages is not None) current_language = self.app.spell_check.current_language() if languages: language_group = QtWidgets.QActionGroup(self) for name, code in languages: if name != code: name = code + ': ' + name language_action = QtWidgets.QAction(name, self) language_action.setCheckable(True) language_action.setChecked(code == current_language) language_action.setData(code) language_action.setActionGroup(language_group) language_menu.addAction(language_action) language_group.triggered.connect(self.set_language) else: language_action = QtWidgets.QAction( self.tr('No dictionary installed'), self) language_action.setEnabled(False) language_menu.addAction(language_action) spelling_menu.addMenu(language_menu) # help menu help_menu = self.menuBar().addMenu(self.tr('Help')) about_action = QtWidgets.QAction(self.tr('About Photini'), self) about_action.triggered.connect(self.about) help_menu.addAction(about_action) help_menu.addSeparator() help_action = QtWidgets.QAction(self.tr('Photini documentation'), self) help_action.triggered.connect(self.open_docs) help_menu.addAction(help_action) # main application area self.central_widget = QtWidgets.QSplitter() self.central_widget.setOrientation(Qt.Vertical) self.central_widget.setChildrenCollapsible(False) self.tabs = QtWidgets.QTabWidget() self.tabs.setTabBar(QTabBar()) self.tabs.setElideMode(Qt.ElideRight) self.tabs.currentChanged.connect(self.new_tab) self.add_tabs(False) self.central_widget.addWidget(self.tabs) self.central_widget.addWidget(self.image_list) size = self.central_widget.sizes() self.central_widget.setSizes(eval( self.app.config_store.get('main_window', 'split', str(size)))) self.central_widget.splitterMoved.connect(self.new_split) self.setCentralWidget(self.central_widget) # open files given on command line, after GUI is displayed self.initial_files = initial_files if self.initial_files: QtCore.QTimer.singleShot(0, self.open_initial_files)
def convert_to_jpeg(self, image): im = QtGui.QImage(image.path) path = self.get_temp_filename(image) im.save(path, format='jpeg', quality=95) self.copy_metadata(image, path) return path
def __init__(self, image_list, parent=None): super(PhotiniMap, self).__init__(parent) self.logger = logging.getLogger(self.__class__.__name__) self.app = QtWidgets.QApplication.instance() self.config_store = self.app.config_store self.image_list = image_list self.script_dir = pkg_resources.resource_filename( 'photini', 'data/' + self.__class__.__name__.lower() + '/') self.drag_icon = QtGui.QPixmap( os.path.join(self.script_dir, 'grey_marker.png')) self.search_string = None self.map_loaded = False self.marker_images = {} self.map_status = {} self.setChildrenCollapsible(False) left_side = QtWidgets.QWidget() self.addWidget(left_side) self.grid = QtWidgets.QGridLayout() self.grid.setContentsMargins(0, 0, 0, 0) self.grid.setRowStretch(6, 1) self.grid.setColumnStretch(1, 1) left_side.setLayout(self.grid) # map self.map = WebView() self.map.setPage(WebPage(parent=self.map)) if QtWebEngineWidgets: self.web_channel = QtWebChannel.QWebChannel() self.map.page().setWebChannel(self.web_channel) self.web_channel.registerObject('python', self) else: self.map.page().setLinkDelegationPolicy( QtWebKitWidgets.QWebPage.DelegateAllLinks) self.map.page().linkClicked.connect(self.link_clicked) self.map.page().mainFrame().javaScriptWindowObjectCleared.connect( self.java_script_window_object_cleared) self.map.setAcceptDrops(False) self.map.drop_text.connect(self.drop_text) self.addWidget(self.map) # search self.grid.addWidget( QtWidgets.QLabel(translate('PhotiniMap', 'Search:')), 0, 0) self.edit_box = QtWidgets.QComboBox() self.edit_box.setMinimumWidth(200) self.edit_box.setEditable(True) self.edit_box.setInsertPolicy(QtWidgets.QComboBox.NoInsert) self.edit_box.lineEdit().setPlaceholderText( translate('PhotiniMap', '<new search>')) self.edit_box.lineEdit().returnPressed.connect(self.search) self.edit_box.activated.connect(self.goto_search_result) self.clear_search() self.edit_box.setEnabled(False) self.grid.addWidget(self.edit_box, 0, 1) # latitude & longitude self.grid.addWidget( QtWidgets.QLabel(translate('PhotiniMap', 'Lat, long:')), 1, 0) self.coords = SingleLineEdit() self.coords.editingFinished.connect(self.new_coords) self.coords.setEnabled(False) self.grid.addWidget(self.coords, 1, 1) # convert lat/lng to location info self.auto_location = QtWidgets.QPushButton( translate('PhotiniMap', 'Lat, long -> location')) self.auto_location.setEnabled(False) self.auto_location.clicked.connect(self.get_address) self.grid.addWidget(self.auto_location, 2, 1) # location info self.location_info = LocationInfo() self.location_info['taken'].new_value.connect(self.new_location_taken) self.location_info['shown'].new_value.connect(self.new_location_shown) self.location_info.swap.clicked.connect(self.swap_locations) self.location_info.setEnabled(False) self.grid.addWidget(self.location_info, 3, 0, 1, 2) # load map button self.load_map = QtWidgets.QPushButton( translate('PhotiniMap', '\nLoad map\n')) self.load_map.clicked.connect(self.initialise) self.grid.addWidget(self.load_map, 7, 0, 1, 2) # other init self.image_list.image_list_changed.connect(self.image_list_changed) self.splitterMoved.connect(self.new_split)