Skip to content

Commit

Permalink
Added preview of output image quality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fincap committed Jun 24, 2021
1 parent fc2c4e8 commit dd8a354
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
17 changes: 17 additions & 0 deletions png-to-jpeg/convert.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()))
18 changes: 16 additions & 2 deletions png-to-jpeg/view.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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 != '':
Expand All @@ -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")
Expand Down

0 comments on commit dd8a354

Please sign in to comment.