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 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 __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)