import sys
|
|
from PySide6.QtGui import QPixmap, QImage
|
#from PIL.ImageQt import QPixmap, QImage
|
|
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog
|
import numpy as np
|
from PIL import Image
|
|
from imgdemo.ui_mainwindow import Ui_MainWindow
|
|
class MainWindow(QMainWindow):
|
def __init__(self):
|
super(MainWindow, self).__init__()
|
self.saved_file = ('', '')
|
self.chosen_file = ('', '')
|
self.ui = Ui_MainWindow()
|
self.ui.setupUi(self)
|
self.ui.actionChoose_Image.triggered.connect(self.choose_image)
|
self.ui.actionSave_Image.triggered.connect(self.save_image)
|
|
def choose_image(self):
|
self.chosen_file = ('', '') # reset (for safe)
|
self.chosen_file = QFileDialog.getOpenFileName(None, "Choose Image", "",
|
"Images (*.png *.xpm *.jpg *.tiff) ;; All Filetypes (*.*)")
|
print(self.chosen_file)
|
if self.chosen_file[0] != '': # user has chosen a file
|
self.display_image()
|
|
|
def save_image(self):
|
self.saved_file = ('', '') # reset for safe
|
self.saved_file = QFileDialog.getSaveFileName(None, "Choose Image", "",
|
"All (*.*)")
|
print(self.saved_file)
|
|
|
def display_image(self):
|
pixmap = QPixmap(self.chosen_file[0])
|
self.ui.origin_img.setPixmap(pixmap)
|
# now read the image and show it via Image in other container
|
with Image.open(self.chosen_file[0]) as img:
|
image = np.asarray(img, dtype=np.uint8)
|
self.display_numpy_image(image)
|
|
|
def display_numpy_image(self, image):
|
height, width, channel = image.shape
|
print(height, width, channel)
|
|
bytes_per_line = channel * width # color
|
img_format = QImage.Format.Format_RGBX8888
|
if channel == 4:
|
img_format = QImage.Format.Format_ARGB32
|
elif channel == 3:
|
img_format = QImage.Format.Format_RGB888
|
qImg = QImage(image.data, width, height, bytes_per_line, img_format).rgbSwapped()
|
pixmap = QPixmap(qImg)
|
self.ui.result_img.setPixmap(pixmap)
|
|
|
|
|
def main():
|
app = QApplication(sys.argv)
|
window = MainWindow()
|
window.show()
|
|
sys.exit(app.exec())
|
pass
|
|
|
if __name__ == "__main__":
|
main()
|