Created
November 4, 2018 21:04
-
-
Save StalkingKillah/6592992ab0a0f8fd8c4d5ada8810414d to your computer and use it in GitHub Desktop.
QLabel for image display, EXIF aware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import * | |
import exifread | |
from PyQt5.QtCore import * | |
from PyQt5.QtGui import * | |
from PyQt5.QtWidgets import * | |
class QExifAwareImage(QLabel): | |
changed = pyqtSignal() | |
_transformers = [ | |
lambda p: p, # Normal | |
lambda p: p.transformed(QTransform().scale(-1, 1)), # Mirrored horizontal | |
lambda p: p.transformed(QTransform().rotate(180)), # Rotated 180 | |
lambda p: p.transformed(QTransform().scale(1, -1)), # Mirrored vertical | |
lambda p: p, # Mirrored horizontal then rotated 90 CCW | |
lambda p: p.transformed(QTransform().rotate(90)), # Rotated 90 CW | |
lambda p: p, # Mirrored horizontal then rotated 90 CW | |
lambda p: p.transformed(QTransform().rotate(-90)) # Rotated 90 CCW | |
] | |
_exif = None | |
def __init__(self, *args): | |
super().__init__(*args) | |
if not self.text(): | |
self.setText("No images loaded") | |
def setImage(self, image: Union[str, QPixmap]) -> QPixmap: | |
if type(image) is str: | |
image = QPixmap(image) | |
image = image.scaledToHeight(self.height()) | |
self.setFixedWidth(image.width()) | |
self.setPixmap(image) | |
self.changed.emit() | |
return image | |
def setImageWithExif(self, image_path: str) -> QPixmap: | |
with open(image_path, "rb") as f: | |
exif = exifread.process_file(f, details=False, stop_tag="Image Orientation") | |
if exif: | |
self._exif = exif | |
orientation = exif.get("Image Orientation") | |
if orientation: | |
return self.setImage(self._transformers[orientation.values[0] - 1](QPixmap(image_path))) | |
return self.setImage(QPixmap(image_path)) | |
def exif(self): | |
return self._exif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment