From dd8a35447e16fa2ec9cf9042b7e7c4da17c14fa0 Mon Sep 17 00:00:00 2001 From: fincap Date: Fri, 25 Jun 2021 02:06:59 +1000 Subject: [PATCH] Added preview of output image quality. --- png-to-jpeg/convert.py | 17 +++++++++++++++++ png-to-jpeg/view.py | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/png-to-jpeg/convert.py b/png-to-jpeg/convert.py index c66d147..0265aa9 100644 --- a/png-to-jpeg/convert.py +++ b/png-to-jpeg/convert.py @@ -1,7 +1,9 @@ import logging import os +from io import BytesIO from PIL import Image +from PyQt5 import QtGui def convert_png_to_jpeg(file_paths: list, destination_folder: str, quality: int): @@ -46,3 +48,18 @@ def get_file_list_from_folder(folder_to_check: str) -> list: file_path_list.append(abs_path) return file_path_list + + +def get_preview_pixmap(filepath: str, quality: int) -> QtGui.QPixmap: + """ + Returns a QPixmap showing the result of a conversion of the given file and given quality. + :param filepath: Path to the PNG file being converted. + :param quality: Quality of the output JPEG file + :return: QPixmap of converted JPEG + """ + + im = Image.open(filepath) + rgb_im = im.convert('RGB') + output = BytesIO() + rgb_im.save(output, format="JPEG", quality=quality) + return QtGui.QPixmap.fromImage(QtGui.QImage.fromData(output.getvalue())) diff --git a/png-to-jpeg/view.py b/png-to-jpeg/view.py index 77fd46d..bfa8c2d 100644 --- a/png-to-jpeg/view.py +++ b/png-to-jpeg/view.py @@ -1,8 +1,8 @@ import os -from PyQt5 import QtWidgets, QtCore +from PyQt5 import QtWidgets, QtCore, QtGui -from convert import get_file_list_from_folder, convert_png_to_jpeg +from convert import get_file_list_from_folder, convert_png_to_jpeg, get_preview_pixmap class MainPage(QtWidgets.QMainWindow): @@ -55,6 +55,11 @@ def __init__(self): button_convert = QtWidgets.QPushButton("Convert") button_convert.clicked.connect(self.on_convert_clicked) + # Selected image preview + layout_preview = QtWidgets.QLabel("Quality preview") + self.quality_preview_label = QtWidgets.QLabel() + self.quality_preview_label.setMaximumSize(250, 250) + # Add widgets to the left panel layout_left_panel.addWidget(label_browse_files) layout_left_panel.addWidget(button_browse_files) @@ -64,6 +69,8 @@ def __init__(self): layout_left_panel.addWidget(label_choose_destination) layout_left_panel.addWidget(widget_output_location) layout_left_panel.addWidget(button_convert) + layout_left_panel.addWidget(layout_preview) + layout_left_panel.addWidget(self.quality_preview_label) # Create right panel right_panel = QtWidgets.QWidget() @@ -73,6 +80,7 @@ def __init__(self): # Found items listview label_found_files = QtWidgets.QLabel("Files to convert") self.list_found_files = QtWidgets.QListWidget() + self.list_found_files.clicked.connect(self.refresh_preview) # Add widgets to right panel layout_right_panel.addWidget(label_found_files) @@ -82,6 +90,11 @@ def __init__(self): central_layout.addWidget(right_panel) self.setCentralWidget(central_widget) + def refresh_preview(self): + if self.list_found_files.currentRow() >= 0: + pixmap = get_preview_pixmap(self.file_list[self.list_found_files.currentRow()], self.slider_quality.value()) + self.quality_preview_label.setPixmap(pixmap) + def on_browse_clicked(self): filepath = QtWidgets.QFileDialog.getExistingDirectory(self, "Open Directory") if filepath != '': @@ -94,6 +107,7 @@ def on_browse_clicked(self): def on_slider_updated(self, value): self.label_quality.setText(str(value)) + self.refresh_preview() def on_output_clicked(self): filepath = QtWidgets.QFileDialog.getExistingDirectory(self, "Open Directory")