Now works with optima35 0.5.0
This commit is contained in:
parent
1bc135e8c3
commit
ad62200bb5
2 changed files with 189 additions and 172 deletions
226
gui.py
226
gui.py
|
@ -26,48 +26,48 @@ from PySide6.QtWidgets import (
|
|||
class Optima35GUI(QMainWindow, Ui_MainWindow):
|
||||
def __init__(self, exif_file):
|
||||
super(Optima35GUI, self).__init__()
|
||||
self.name = "GUI35"
|
||||
self.version = "0.1.0"
|
||||
self.ui = Ui_MainWindow()
|
||||
self.ui.setupUi(self)
|
||||
self.o = OPTIMA35()
|
||||
self.u = Utilities()
|
||||
self.exif_file = exif_file
|
||||
self.exif_data = None
|
||||
|
||||
self.setWindowTitle(f"{self.o.name} {self.o.version}")
|
||||
self.default_ui_layout()
|
||||
self.define_gui_interaction()
|
||||
self.available_exif_data = None
|
||||
self.settings = {}
|
||||
self.setWindowTitle(f"{self.name} v{self.version} for {self.o.name} {self.o.version}")
|
||||
self._default_ui_layout()
|
||||
self._define_gui_interaction()
|
||||
|
||||
if exif_file == "config/exif_example.yaml":
|
||||
self.change_statusbar("Using example exif...", 10000)
|
||||
self._change_statusbar("Using example exif...", 10000)
|
||||
|
||||
def default_ui_layout(self):
|
||||
def _default_ui_layout(self):
|
||||
self.ui.png_quality_spinBox.setVisible(False)
|
||||
|
||||
def define_gui_interaction(self):
|
||||
self.ui.input_folder_button.clicked.connect(self.browse_input_folder)
|
||||
self.ui.output_folder_button.clicked.connect(self.browse_output_folder)
|
||||
self.ui.start_button.clicked.connect(self.process)
|
||||
self.ui.image_type.currentIndexChanged.connect(self.update_quality_options)
|
||||
def _define_gui_interaction(self):
|
||||
self.ui.input_folder_button.clicked.connect(self._browse_input_folder)
|
||||
self.ui.output_folder_button.clicked.connect(self._browse_output_folder)
|
||||
self.ui.start_button.clicked.connect(self._process)
|
||||
self.ui.image_type.currentIndexChanged.connect(self._update_quality_options)
|
||||
|
||||
self.ui.exif_checkbox.stateChanged.connect(
|
||||
lambda state: self.handle_checkbox_state(state, 2, self.populate_exif)
|
||||
lambda state: self._handle_checkbox_state(state, 2, self._populate_exif)
|
||||
)
|
||||
self.ui.tabWidget.currentChanged.connect(self.on_tab_changed)
|
||||
self.ui.edit_exif_button.clicked.connect(self.open_exif_editor)
|
||||
self.ui.restart_button.clicked.connect(self.restart_app)
|
||||
self.ui.tabWidget.currentChanged.connect(self._on_tab_changed)
|
||||
self.ui.edit_exif_button.clicked.connect(self._open_exif_editor)
|
||||
|
||||
def process(self):
|
||||
#self.ui.start_button.setEnabled(False)
|
||||
#self.ui.restart_button.setEnabled(False)
|
||||
self.check_options() # Get all user selected data
|
||||
input_folder_valid = os.path.exists(self.o.settings["input_folder"])
|
||||
output_folder_valid = os.path.exists(self.o.settings["output_folder"])
|
||||
def _process(self):
|
||||
self.ui.start_button.setEnabled(False)
|
||||
self._update_settings() # Get all user selected data
|
||||
input_folder_valid = os.path.exists(self.settings["input_folder"])
|
||||
output_folder_valid = os.path.exists(self.settings["output_folder"])
|
||||
if not input_folder_valid or not output_folder_valid:
|
||||
QMessageBox.warning(self, "Warning", f"Input location {input_folder_valid}\nOutput folder {output_folder_valid}...")
|
||||
return
|
||||
|
||||
input_folder = self.o.settings["input_folder"]
|
||||
output_folder = self.o.settings["output_folder"]
|
||||
input_folder = self.settings["input_folder"]
|
||||
output_folder = self.settings["output_folder"]
|
||||
|
||||
image_files = [
|
||||
f for f in os.listdir(input_folder) if f.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
|
||||
|
@ -75,52 +75,66 @@ class Optima35GUI(QMainWindow, Ui_MainWindow):
|
|||
i = 1
|
||||
for image_file in image_files:
|
||||
input_path = os.path.join(input_folder, image_file)
|
||||
if self.o.settings["new_file_names"] != False:
|
||||
image_name = self.o.name_images(self.o.settings["new_file_names"], i, len(image_files), self.o.settings["invert_image_order"])
|
||||
if self.settings["new_file_names"] != False:
|
||||
image_name = self.u.append_number_to_name(self.settings["new_file_names"], i, len(image_files), self.settings["invert_image_order"])
|
||||
else:
|
||||
image_name = os.path.splitext(image_file)[0]
|
||||
output_path = os.path.join(output_folder, image_name)
|
||||
|
||||
self.o.process(input_path, output_path)
|
||||
self.handle_qprogressbar(i, len(image_files))
|
||||
self.o.process_image(
|
||||
image_input_file = input_path,
|
||||
image_output_file = output_path,
|
||||
file_type = self.settings["file_format"],
|
||||
quality = self.settings["jpg_quality"],
|
||||
compressing = self.settings["png_compression"],
|
||||
optimize = self.ui.optimize_checkBox.isChecked(),
|
||||
resize = self.settings["resize"],
|
||||
watermark = self.settings["watermark"],
|
||||
font_size = self.settings["font_size"],
|
||||
grayscale = self.settings["grayscale"],
|
||||
brightness = self.settings["brightness"],
|
||||
contrast = self.settings["contrast"],
|
||||
dict_for_exif = self.user_selected_exif,
|
||||
gps = self.settings["gps"],
|
||||
copy_exif = self.settings["copy_exif"])
|
||||
self._handle_qprogressbar(i, len(image_files))
|
||||
i += 1
|
||||
|
||||
QMessageBox.information(self, "Information", "Finished")
|
||||
#self.ui.start_button.setEnabled(True)
|
||||
#self.ui.restart_button.setEnabled(True)
|
||||
self.ui.start_button.setEnabled(True)
|
||||
self.ui.progressBar.setValue(0)
|
||||
|
||||
def open_exif_editor(self):
|
||||
def _open_exif_editor(self):
|
||||
"""Open the EXIF Editor."""
|
||||
self.exif_editor = ExifEditor(self.exif_data)
|
||||
self.exif_editor.exif_data_updated.connect(self.update_exif_data)
|
||||
self.exif_editor = ExifEditor(self.available_exif_data)
|
||||
self.exif_editor.exif_data_updated.connect(self._update_exif_data)
|
||||
self.exif_editor.show()
|
||||
|
||||
def update_exif_data(self, updated_exif_data):
|
||||
def _update_exif_data(self, updated_exif_data):
|
||||
"""Update the EXIF data."""
|
||||
self.exif_data = updated_exif_data
|
||||
self.populate_exif()
|
||||
self._populate_exif()
|
||||
|
||||
def handle_checkbox_state(self, state, desired_state, action):
|
||||
def _handle_checkbox_state(self, state, desired_state, action):
|
||||
"""Perform an action based on the checkbox state and a desired state. Have to use lambda when calling."""
|
||||
if state == desired_state:
|
||||
action()
|
||||
|
||||
def on_tab_changed(self, index):
|
||||
def _on_tab_changed(self, index):
|
||||
"""Handle tab changes."""
|
||||
# chatgpt
|
||||
if index == 1: # EXIF Tab
|
||||
self.handle_exif_file("read")
|
||||
self._handle_exif_file("read")
|
||||
elif index == 0: # Main Tab
|
||||
self.handle_exif_file("write")
|
||||
self._handle_exif_file("write")
|
||||
|
||||
def handle_exif_file(self, do):
|
||||
def _handle_exif_file(self, do):
|
||||
if do == "read":
|
||||
self.exif_data = self.u.read_yaml(self.exif_file)
|
||||
self.available_exif_data = self.u.read_yaml(self.exif_file)
|
||||
elif do == "write":
|
||||
self.u.write_yaml(self.exif_file, self.exif_data)
|
||||
self.u.write_yaml(self.exif_file, self.available_exif_data)
|
||||
|
||||
def populate_exif(self):
|
||||
def _populate_exif(self):
|
||||
# partly chatGPT
|
||||
# Mapping of EXIF fields to comboboxes in the UI
|
||||
combo_mapping = {
|
||||
|
@ -133,16 +147,16 @@ class Optima35GUI(QMainWindow, Ui_MainWindow):
|
|||
"artist": self.ui.artist_comboBox,
|
||||
"copyright_info": self.ui.copyright_info_comboBox,
|
||||
}
|
||||
self.populate_comboboxes(combo_mapping)
|
||||
self._populate_comboboxes(combo_mapping)
|
||||
|
||||
def populate_comboboxes(self, combo_mapping):
|
||||
def _populate_comboboxes(self, combo_mapping):
|
||||
"""Populate comboboxes with EXIF data."""
|
||||
# ChatGPT
|
||||
for field, comboBox in combo_mapping.items():
|
||||
comboBox.clear() # Clear existing items
|
||||
comboBox.addItems(map(str, self.exif_data.get(field, [])))
|
||||
comboBox.addItems(map(str, self.available_exif_data.get(field, [])))
|
||||
|
||||
def update_quality_options(self):
|
||||
def _update_quality_options(self):
|
||||
"""Update visibility of quality settings based on selected format."""
|
||||
# ChatGPT
|
||||
selected_format = self.ui.image_type.currentText()
|
||||
|
@ -157,82 +171,88 @@ class Optima35GUI(QMainWindow, Ui_MainWindow):
|
|||
elif selected_format == "png":
|
||||
self.ui.png_quality_spinBox.setVisible(True)
|
||||
|
||||
def browse_input_folder(self):
|
||||
def _browse_input_folder(self):
|
||||
folder = QFileDialog.getExistingDirectory(self, "Select Input Folder")
|
||||
if folder:
|
||||
self.ui.input_path.setText(folder)
|
||||
|
||||
def browse_output_folder(self):
|
||||
def _browse_output_folder(self):
|
||||
folder = QFileDialog.getExistingDirectory(self, "Select Output Folder")
|
||||
if folder:
|
||||
self.ui.output_path.setText(folder)
|
||||
|
||||
def change_statusbar(self, msg, timeout = 500):
|
||||
def _change_statusbar(self, msg, timeout = 500):
|
||||
self.ui.statusBar.showMessage(msg, timeout)
|
||||
|
||||
def handle_qprogressbar(self, current, total):
|
||||
def _handle_qprogressbar(self, current, total):
|
||||
progress = int((100 / total) * current)
|
||||
self.ui.progressBar.setValue(progress)
|
||||
|
||||
def check_options(self):
|
||||
try:
|
||||
self.o.settings["input_folder"] = self.ui.input_path.text()
|
||||
self.o.settings["output_folder"] = self.ui.output_path.text()
|
||||
self.o.settings["file_format"] = self.ui.image_type.currentText()
|
||||
self.o.settings["jpg_quality"] = int(self.ui.jpg_quality_spinBox.text())
|
||||
self.o.settings["png_compression"] = int(self.ui.png_quality_spinBox.text())
|
||||
self.o.settings["invert_image_order"] = self.ui.revert_checkbox.isChecked()
|
||||
self.o.settings["grayscale"] = self.ui.grayscale_checkBox.isChecked()
|
||||
self.o.settings["copy_exif"] = self.ui.exif_copy_checkBox.isChecked()
|
||||
self.o.settings["own_exif"] = self.ui.exif_checkbox.isChecked()
|
||||
self.o.settings["font_size"] = self.ui.font_size_comboBox.currentIndex() + 1
|
||||
self.o.settings["optimize"] = self.ui.optimize_checkBox.isChecked()
|
||||
self.o.settings["own_date"] = self.ui.add_date_checkBox.isChecked()
|
||||
def _get_checkbox_value(self, checkbox, default=None):
|
||||
"""Helper function to get the value of a checkbox or a default value."""
|
||||
return checkbox.isChecked() if checkbox else default
|
||||
|
||||
if self.ui.resize_checkbox.isChecked():
|
||||
self.o.settings["resize"] = int(self.ui.resize_spinBox.text())
|
||||
def _get_spinbox_value(self, spinbox, default=None):
|
||||
"""Helper function to get the value of a spinbox and handle empty input."""
|
||||
return int(spinbox.text()) if spinbox.text() else default
|
||||
|
||||
if self.ui.brightness_checkbox.isChecked():
|
||||
self.o.settings["brightness"] = int(self.ui.brightness_spinBox.text())
|
||||
def _get_combobox_value(self, combobox, default=None):
|
||||
"""Helper function to get the value of a combobox."""
|
||||
return combobox.currentIndex() + 1 if combobox.currentIndex() != -1 else default
|
||||
|
||||
if self.ui.contrast_checkbox.isChecked():
|
||||
self.o.settings["contrast"] = int(self.ui.contrast_spinBox.text())
|
||||
def _get_text_value(self, lineedit, default=None):
|
||||
"""Helper function to get the value of a text input field."""
|
||||
return lineedit.text() if lineedit.text() else default
|
||||
|
||||
if self.ui.rename_checkbox.isChecked():
|
||||
if self.ui.filename.text() != "":
|
||||
self.o.settings["new_file_names"] = self.ui.filename.text()
|
||||
else:
|
||||
self.o.settings["new_file_names"] = False
|
||||
else:
|
||||
self.o.settings["new_file_names"] = False
|
||||
def _get_selected_exif(self):
|
||||
"""Collect selected EXIF data and handle date and GPS if necessary."""
|
||||
selected_exif = self._collect_selected_exif() if self.ui.exif_checkbox.isChecked() else None
|
||||
if selected_exif:
|
||||
if self.ui.add_date_checkBox.isChecked():
|
||||
selected_exif["date_time_original"] = self._get_date()
|
||||
if self.ui.gps_checkBox.isChecked():
|
||||
self.settings["gps"] = [self.ui.lat_lineEdit.text(), self.ui.long_lineEdit.text()]
|
||||
else:
|
||||
self.settings["gps"] = None
|
||||
return selected_exif
|
||||
|
||||
def _update_settings(self):
|
||||
"""Update .settings from all GUI elements."""
|
||||
# General settings
|
||||
self.settings["input_folder"] = self._get_text_value(self.ui.input_path)
|
||||
self.settings["output_folder"] = self._get_text_value(self.ui.output_path)
|
||||
self.settings["file_format"] = self.ui.image_type.currentText()
|
||||
self.settings["jpg_quality"] = self._get_spinbox_value(self.ui.jpg_quality_spinBox)
|
||||
self.settings["png_compression"] = self._get_spinbox_value(self.ui.png_quality_spinBox)
|
||||
self.settings["invert_image_order"] = self._get_checkbox_value(self.ui.revert_checkbox)
|
||||
self.settings["grayscale"] = self._get_checkbox_value(self.ui.grayscale_checkBox)
|
||||
self.settings["copy_exif"] = self._get_checkbox_value(self.ui.exif_copy_checkBox)
|
||||
self.settings["own_exif"] = self._get_checkbox_value(self.ui.exif_checkbox)
|
||||
self.settings["font_size"] = self._get_combobox_value(self.ui.font_size_comboBox)
|
||||
self.settings["optimize"] = self._get_checkbox_value(self.ui.optimize_checkBox)
|
||||
self.settings["own_date"] = self._get_checkbox_value(self.ui.add_date_checkBox)
|
||||
|
||||
if self.ui.watermark_checkbox.isChecked():
|
||||
if self.ui.watermark_lineEdit.text() != "":
|
||||
self.o.settings["watermark"] = self.ui.watermark_lineEdit.text()
|
||||
else:
|
||||
self.o.settings["watermark"] = False
|
||||
else:
|
||||
self.o.settings["watermark"] = False
|
||||
# Conditional settings with logic
|
||||
self.settings["resize"] = self._get_spinbox_value(self.ui.resize_spinBox) if self.ui.resize_checkbox.isChecked() else None
|
||||
self.settings["brightness"] = self._get_spinbox_value(self.ui.brightness_spinBox) if self.ui.brightness_checkbox.isChecked() else None
|
||||
self.settings["contrast"] = self._get_spinbox_value(self.ui.contrast_spinBox) if self.ui.contrast_checkbox.isChecked() else None
|
||||
|
||||
if self.o.settings["own_exif"]:
|
||||
self.o.selected_exif = self.collect_selected_exif()
|
||||
if self.ui.add_date_checkBox.isChecked():
|
||||
self.o.selected_exif["date_time_original"] = self.get_date()
|
||||
if self.ui.gps_checkBox.isChecked():
|
||||
self.o.settings["gps"] = [self.ui.lat_lineEdit.text(), self.ui.long_lineEdit.text()]
|
||||
else:
|
||||
self.o.settings["gps"] = False
|
||||
self.settings["new_file_names"] = self._get_text_value(self.ui.filename, False) if self.ui.rename_checkbox.isChecked() else False
|
||||
self.settings["watermark"] = self._get_text_value(self.ui.watermark_lineEdit) if self.ui.watermark_checkbox.isChecked() else None
|
||||
|
||||
except Exception as e:
|
||||
print(f"Whoops: {e}")
|
||||
# Handle EXIF data selection
|
||||
if self.settings["own_exif"]:
|
||||
self.user_selected_exif = self._get_selected_exif()
|
||||
else:
|
||||
self.user_selected_exif = None
|
||||
self.settings["gps"] = None
|
||||
|
||||
def get_date(self):
|
||||
def _get_date(self):
|
||||
date_input = self.ui.dateEdit.date().toString("yyyy-MM-dd")
|
||||
new_date = datetime.strptime(date_input, "%Y-%m-%d")
|
||||
return new_date.strftime("%Y:%m:%d 00:00:00")
|
||||
|
||||
def collect_selected_exif(self):
|
||||
def _collect_selected_exif(self):
|
||||
user_data = {}
|
||||
user_data["make"] = self.ui.make_comboBox.currentText()
|
||||
user_data["model"] = self.ui.model_comboBox.currentText()
|
||||
|
@ -245,18 +265,6 @@ class Optima35GUI(QMainWindow, Ui_MainWindow):
|
|||
user_data["software"] = f"{self.o.name} {self.o.version}"
|
||||
return user_data
|
||||
|
||||
def rebuild_ui(self):
|
||||
# Define the bash script to execute
|
||||
bash_script = "/home/sam/git/gitlab_public/optima-35/rebuild_ui.sh"
|
||||
os.system(bash_script)
|
||||
|
||||
def restart_app(self):
|
||||
"""Restarts the application."""
|
||||
self.rebuild_ui()
|
||||
# chatGPT
|
||||
python = sys.executable # Path to the Python interpreter
|
||||
os.execv(python, [python] + sys.argv)
|
||||
|
||||
def main(exif_file):
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
window = Optima35GUI(exif_file=exif_file)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue