You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
photo/PhotoProcessor.py

105 lines
2.8 KiB

import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import *
import PyQt6.QtGui as QtGui
from PyQt6.QtGui import QColor, QAction, QPixmap, QPalette
from pathlib import Path
import cv2
import numpy as np
from Photo import Photo
class PhotoProcessor(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.labelImage = QLabel(self)
# self.labelImage.setScaledContents(True)
self.initMenu()
self.showMaximized()
def initMenu(self):
self.statusBar()
menubar = self.menuBar()
self.fileMenu = menubar.addMenu('File')
self.editMenu = menubar.addMenu('Edit')
self.initFileMenu()
self.initEditMenu()
def initEditMenu(self):
rotate = QAction('Rotate', self)
rotate.triggered.connect(self.rotate)
resize = QAction('Resize', self)
resize.triggered.connect(self.resize)
self.editMenu.addActions([rotate])
def resize(self):
# self.Image.rotate(ratio)
self.display_image()
def rotate(self):
self.Image.rotate()
self.display_image()
def initFileMenu(self):
openFile = QAction('Open image', self)
openFile.triggered.connect(self.openImage)
saveFile = QAction('Save image', self)
self.fileMenu.addActions([openFile, saveFile])
def openImage(self, e):
home_dir = str(Path.home())
filepath = QFileDialog.getOpenFileName(self, 'Open Image', home_dir, "Image files(*.jpg *.png)")
if filepath[0]:
self.Image = Photo(filepath[0])
self.display_image()
def display_image(self):
shrink = cv2.cvtColor(self.Image.img, cv2.COLOR_BGR2RGB)
qt_img = QtGui.QImage(shrink.data,
shrink.shape[1],
shrink.shape[0],
shrink.shape[1] * 3,
QtGui.QImage.Format.Format_RGB888)
print(qt_img.width(), qt_img.height())
w, h = qt_img.width(), qt_img.height()
f1 = 1.0 * self.width() / w
f2 = 1.0 * self.height() / h
factor = min([f1, f2])
width = int(w * factor)
height = int(h * factor)
self.labelImage.setPixmap(QPixmap.fromImage(qt_img).scaled(width, height))
self.labelImage.resize(width, height)
# self.image_resize()
def image_resize(self):
w, h = self.labelImage.width(), self.labelImage.height()
f1 = 1.0 * self.width() / w
f2 = 1.0 * self.height() / h
factor = min([f1, f2])
width = int(w * factor)
height = int(h * factor)
self.labelImage.resize(width, height)
def run():
app = QApplication(sys.argv)
pp = PhotoProcessor()
sys.exit(app.exec())
run()