Enhance image processing: update resize filter handling and improve product search logic
This commit is contained in:
@@ -7,7 +7,7 @@ from config.encrypt_config import ConfigEncryptor
|
|||||||
from api.woocommerce_api import get_first_image
|
from api.woocommerce_api import get_first_image
|
||||||
from PIL import Image, ImageTk
|
from PIL import Image, ImageTk
|
||||||
from pprint import pformat
|
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 customtkinter as ctk
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -271,8 +271,8 @@ class AppController:
|
|||||||
self.preview_bar.before_filename_label.configure(text=dir_name)
|
self.preview_bar.before_filename_label.configure(text=dir_name)
|
||||||
|
|
||||||
if first_image_path:
|
if first_image_path:
|
||||||
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
fd, output_path = tempfile.mkstemp(suffix=".jpg")
|
||||||
output_path = temp_file.name
|
os.close(fd)
|
||||||
self.image.resize_image(
|
self.image.resize_image(
|
||||||
first_image_path, output_path, self.get_options()
|
first_image_path, output_path, self.get_options()
|
||||||
)
|
)
|
||||||
@@ -287,7 +287,6 @@ class AppController:
|
|||||||
after_photo = ImageTk.PhotoImage(after_img)
|
after_photo = ImageTk.PhotoImage(after_img)
|
||||||
self.preview_bar.after_image_label.configure(image=after_photo)
|
self.preview_bar.after_image_label.configure(image=after_photo)
|
||||||
self.preview_bar.after_image_label.image = after_photo
|
self.preview_bar.after_image_label.image = after_photo
|
||||||
|
|
||||||
name = self.file.generate_output_path("/",first_image_path,self.get_options())
|
name = self.file.generate_output_path("/",first_image_path,self.get_options())
|
||||||
dir_name = os.path.basename(name)
|
dir_name = os.path.basename(name)
|
||||||
if len(dir_name) > 35:
|
if len(dir_name) > 35:
|
||||||
@@ -474,7 +473,17 @@ class AppController:
|
|||||||
self.update_previews()
|
self.update_previews()
|
||||||
|
|
||||||
def process_product(self, input):
|
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:
|
if self.found_products:
|
||||||
count_products = len(self.found_products)
|
count_products = len(self.found_products)
|
||||||
print(f"Found {count_products} products")
|
print(f"Found {count_products} products")
|
||||||
@@ -486,12 +495,19 @@ class AppController:
|
|||||||
text = f"Viewing product {number}/{count_products}"
|
text = f"Viewing product {number}/{count_products}"
|
||||||
self.info_bar.destination_label.configure(text=text)
|
self.info_bar.destination_label.configure(text=text)
|
||||||
self.update_previews()
|
self.update_previews()
|
||||||
|
self.update_product_nav_buttons()
|
||||||
return self.found_products[self.current_product]
|
return self.found_products[self.current_product]
|
||||||
|
self.info_bar.destination_label.configure(text="No products found")
|
||||||
|
self.update_product_nav_buttons()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def change_product(self, data):
|
def change_product(self, data):
|
||||||
self.current_product += data
|
if not self.found_products:
|
||||||
if self.found_products and len(self.found_products) >= self.current_product:
|
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)
|
count_products = len(self.found_products)
|
||||||
print(self.found_products[self.current_product])
|
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'])+")" )
|
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}"
|
text = f"Viewing product {number}/{count_products}"
|
||||||
self.info_bar.destination_label.configure(text=text)
|
self.info_bar.destination_label.configure(text=text)
|
||||||
self.update_previews()
|
self.update_previews()
|
||||||
|
self.update_product_nav_buttons()
|
||||||
pass
|
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):
|
def update_info(self, selected_option):
|
||||||
"""
|
"""
|
||||||
Update the info frame based on the 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.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.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":
|
elif selected_option == "file":
|
||||||
self.info_bar.description_label.configure(text="Choose a file to process:")
|
self.info_bar.description_label.configure(text="Choose a file to process:")
|
||||||
|
|||||||
@@ -54,15 +54,25 @@ from collections import deque
|
|||||||
|
|
||||||
NS_DEEPZOOM = "http://schemas.microsoft.com/deepzoom/2008"
|
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"
|
DEFAULT_IMAGE_FORMAT = "jpg"
|
||||||
|
|
||||||
RESIZE_FILTERS = {
|
RESIZE_FILTERS = {
|
||||||
"cubic": PIL.Image.CUBIC,
|
"cubic": _BICUBIC,
|
||||||
"bilinear": PIL.Image.BILINEAR,
|
"bilinear": _BILINEAR,
|
||||||
"bicubic": PIL.Image.BICUBIC,
|
"bicubic": _BICUBIC,
|
||||||
"nearest": PIL.Image.NEAREST,
|
"nearest": _NEAREST,
|
||||||
"antialias": PIL.Image.ANTIALIAS,
|
"antialias": _LANCZOS,
|
||||||
}
|
}
|
||||||
|
|
||||||
IMAGE_FORMATS = {
|
IMAGE_FORMATS = {
|
||||||
@@ -311,14 +321,14 @@ class DeepZoomCollection(object):
|
|||||||
if w != e_w or h != e_h:
|
if w != e_w or h != e_h:
|
||||||
# Resize incorrect tile to correct size
|
# Resize incorrect tile to correct size
|
||||||
source_image = source_image.resize(
|
source_image = source_image.resize(
|
||||||
(e_w, e_h), PIL.Image.ANTIALIAS
|
(e_w, e_h), DEFAULT_RESIZE_FILTER
|
||||||
)
|
)
|
||||||
# Store new dimensions
|
# Store new dimensions
|
||||||
w, h = e_w, e_h
|
w, h = e_w, e_h
|
||||||
else:
|
else:
|
||||||
w = int(math.ceil(w * 0.5))
|
w = int(math.ceil(w * 0.5))
|
||||||
h = int(math.ceil(h * 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)
|
column, row = self.get_position(i)
|
||||||
x = (column % images_per_tile) * level_size
|
x = (column % images_per_tile) * level_size
|
||||||
y = (row % 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:
|
if self.descriptor.width == width and self.descriptor.height == height:
|
||||||
return self.image
|
return self.image
|
||||||
if (self.resize_filter is None) or (self.resize_filter not in RESIZE_FILTERS):
|
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])
|
return self.image.resize((width, height), RESIZE_FILTERS[self.resize_filter])
|
||||||
|
|
||||||
def tiles(self, level):
|
def tiles(self, level):
|
||||||
|
|||||||
Reference in New Issue
Block a user