Enhance image processing: update resize filter handling and improve product search logic

This commit is contained in:
SitiWeb
2026-01-13 19:18:32 +01:00
parent e15e337dbe
commit d6a6fd6f9f
2 changed files with 67 additions and 30 deletions

View File

@@ -7,7 +7,7 @@ from config.encrypt_config import ConfigEncryptor
from api.woocommerce_api import get_first_image
from PIL import Image, ImageTk
from pprint import pformat
from api.woocommerce_api import process_product_images, process_all_products, search_product, get_first_image_path
from api.woocommerce_api import process_product_images, process_all_products, search_product, get_first_image_path, get_product
import customtkinter as ctk
import os
@@ -271,23 +271,22 @@ class AppController:
self.preview_bar.before_filename_label.configure(text=dir_name)
if first_image_path:
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
output_path = temp_file.name
self.image.resize_image(
first_image_path, output_path, self.get_options()
)
before_img = Image.open(first_image_path)
before_img.thumbnail((200, 200))
before_photo = ImageTk.PhotoImage(before_img)
self.preview_bar.before_image_label.configure(image=before_photo)
self.preview_bar.before_image_label.image = before_photo
fd, output_path = tempfile.mkstemp(suffix=".jpg")
os.close(fd)
self.image.resize_image(
first_image_path, output_path, self.get_options()
)
before_img = Image.open(first_image_path)
before_img.thumbnail((200, 200))
before_photo = ImageTk.PhotoImage(before_img)
self.preview_bar.before_image_label.configure(image=before_photo)
self.preview_bar.before_image_label.image = before_photo
after_img = Image.open(output_path)
after_img.thumbnail((200, 200))
after_photo = ImageTk.PhotoImage(after_img)
self.preview_bar.after_image_label.configure(image=after_photo)
self.preview_bar.after_image_label.image = after_photo
after_img = Image.open(output_path)
after_img.thumbnail((200, 200))
after_photo = ImageTk.PhotoImage(after_img)
self.preview_bar.after_image_label.configure(image=after_photo)
self.preview_bar.after_image_label.image = after_photo
name = self.file.generate_output_path("/",first_image_path,self.get_options())
dir_name = os.path.basename(name)
if len(dir_name) > 35:
@@ -474,7 +473,17 @@ class AppController:
self.update_previews()
def process_product(self, input):
self.found_products = search_product(input)
cleaned_input = (input or "").strip()
self.found_products = None
self.current_product = 0
if cleaned_input.isdigit():
product = get_product(int(cleaned_input))
if product:
self.found_products = [product]
else:
self.found_products = search_product(cleaned_input)
if self.found_products:
count_products = len(self.found_products)
print(f"Found {count_products} products")
@@ -486,12 +495,19 @@ class AppController:
text = f"Viewing product {number}/{count_products}"
self.info_bar.destination_label.configure(text=text)
self.update_previews()
self.update_product_nav_buttons()
return self.found_products[self.current_product]
self.info_bar.destination_label.configure(text="No products found")
self.update_product_nav_buttons()
pass
def change_product(self, data):
self.current_product += data
if self.found_products and len(self.found_products) >= self.current_product:
if not self.found_products:
self.update_product_nav_buttons()
return
count_products = len(self.found_products)
self.current_product = max(0, min(self.current_product + data, count_products - 1))
if count_products > 0:
count_products = len(self.found_products)
print(self.found_products[self.current_product])
self.info_bar.selected_button_label.configure(text=self.found_products[self.current_product]['name'] + " (id: "+ str(self.found_products[self.current_product]['id'])+")" )
@@ -500,8 +516,18 @@ class AppController:
text = f"Viewing product {number}/{count_products}"
self.info_bar.destination_label.configure(text=text)
self.update_previews()
self.update_product_nav_buttons()
pass
def update_product_nav_buttons(self):
if not getattr(self.info_bar, "next_button", None) or not getattr(self.info_bar, "prev_button", None):
return
total = len(self.found_products) if self.found_products else 0
can_prev = total > 0 and self.current_product > 0
can_next = total > 0 and self.current_product < (total - 1)
self.info_bar.prev_button.configure(state="normal" if can_prev else "disabled")
self.info_bar.next_button.configure(state="normal" if can_next else "disabled")
def update_info(self, selected_option):
"""
Update the info frame based on the selected option.
@@ -559,6 +585,7 @@ class AppController:
self.info_bar.parent_frame, text="No products found"
)
self.info_bar.destination_label.grid(row=2, column=0, columnspan=2, padx=5, pady=5, sticky="w")
self.update_product_nav_buttons()
elif selected_option == "file":
self.info_bar.description_label.configure(text="Choose a file to process:")

View File

@@ -54,15 +54,25 @@ from collections import deque
NS_DEEPZOOM = "http://schemas.microsoft.com/deepzoom/2008"
DEFAULT_RESIZE_FILTER = PIL.Image.ANTIALIAS
if hasattr(PIL.Image, "Resampling"):
_RESAMPLING = PIL.Image.Resampling
else:
_RESAMPLING = PIL.Image
_LANCZOS = _RESAMPLING.LANCZOS
_BICUBIC = _RESAMPLING.BICUBIC
_BILINEAR = _RESAMPLING.BILINEAR
_NEAREST = _RESAMPLING.NEAREST
DEFAULT_RESIZE_FILTER = _LANCZOS
DEFAULT_IMAGE_FORMAT = "jpg"
RESIZE_FILTERS = {
"cubic": PIL.Image.CUBIC,
"bilinear": PIL.Image.BILINEAR,
"bicubic": PIL.Image.BICUBIC,
"nearest": PIL.Image.NEAREST,
"antialias": PIL.Image.ANTIALIAS,
"cubic": _BICUBIC,
"bilinear": _BILINEAR,
"bicubic": _BICUBIC,
"nearest": _NEAREST,
"antialias": _LANCZOS,
}
IMAGE_FORMATS = {
@@ -311,14 +321,14 @@ class DeepZoomCollection(object):
if w != e_w or h != e_h:
# Resize incorrect tile to correct size
source_image = source_image.resize(
(e_w, e_h), PIL.Image.ANTIALIAS
(e_w, e_h), DEFAULT_RESIZE_FILTER
)
# Store new dimensions
w, h = e_w, e_h
else:
w = int(math.ceil(w * 0.5))
h = int(math.ceil(h * 0.5))
source_image.thumbnail((w, h), PIL.Image.ANTIALIAS)
source_image.thumbnail((w, h), DEFAULT_RESIZE_FILTER)
column, row = self.get_position(i)
x = (column % images_per_tile) * level_size
y = (row % images_per_tile) * level_size
@@ -407,7 +417,7 @@ class ImageCreator(object):
if self.descriptor.width == width and self.descriptor.height == height:
return self.image
if (self.resize_filter is None) or (self.resize_filter not in RESIZE_FILTERS):
return self.image.resize((width, height), PIL.Image.ANTIALIAS)
return self.image.resize((width, height), DEFAULT_RESIZE_FILTER)
return self.image.resize((width, height), RESIZE_FILTERS[self.resize_filter])
def tiles(self, level):
@@ -648,4 +658,4 @@ def main():
if __name__ == "__main__":
main()
main()