diff --git a/py/IMAGE_GPT2_apikey.py b/py/IMAGE_GPT2_apikey.py index 83d7035..dd67dc9 100644 --- a/py/IMAGE_GPT2_apikey.py +++ b/py/IMAGE_GPT2_apikey.py @@ -61,6 +61,10 @@ class PDOpenAIGPTImage2APIKey: "default": "1024x1024", "tooltip": "Preset output size" }), + "use_custom_size": ("BOOLEAN", { + "default": False, + "tooltip": "Enable custom_width/custom_height size override." + }), "custom_width": ("INT", { "default": 0, "min": 0, @@ -113,6 +117,7 @@ class PDOpenAIGPTImage2APIKey: quality, background, size, + use_custom_size, custom_width, custom_height, num_images, @@ -127,7 +132,12 @@ class PDOpenAIGPTImage2APIKey: return (empty_image(), error_msg, "") try: - resolved_size = validate_gpt_image_2_size(size, custom_width, custom_height) + resolved_size = validate_gpt_image_2_size( + size, + custom_width, + custom_height, + use_custom_size=use_custom_size, + ) except Exception as e: return (empty_image(), f"Error: {str(e)}", "") @@ -191,7 +201,12 @@ class PDOpenAIGPTImage2APIKey: duration = time.time() - start_time min_cost, max_cost = estimate_price_range_2(quality, num_images) token_cost = calculate_token_price_2(num_images) - size_note = resolve_gpt_image_2_size(size, custom_width, custom_height) + size_note = resolve_gpt_image_2_size( + size, + custom_width, + custom_height, + use_custom_size=use_custom_size, + ) exact_cost = calculate_exact_cost_gpt_image_2_from_usage(result) mode_label = "编辑" if is_edit_mode else "生成" diff --git a/py/IMAGE_GPT2_authtoken.py b/py/IMAGE_GPT2_authtoken.py index 257571e..8349cb8 100644 --- a/py/IMAGE_GPT2_authtoken.py +++ b/py/IMAGE_GPT2_authtoken.py @@ -55,6 +55,10 @@ class PDOpenAIGPTImage2AuthToken: "default": "1024x1024", "tooltip": "Preset output size" }), + "use_custom_size": ("BOOLEAN", { + "default": False, + "tooltip": "Enable custom_width/custom_height size override." + }), "custom_width": ("INT", { "default": 0, "min": 0, @@ -105,6 +109,7 @@ class PDOpenAIGPTImage2AuthToken: quality, background, size, + use_custom_size, custom_width, custom_height, num_images, @@ -120,7 +125,12 @@ class PDOpenAIGPTImage2AuthToken: return (empty_image(), error_msg, "") try: - resolved_size = validate_gpt_image_2_size(size, custom_width, custom_height) + resolved_size = validate_gpt_image_2_size( + size, + custom_width, + custom_height, + use_custom_size=use_custom_size, + ) except Exception as e: return (empty_image(), f"Error: {str(e)}", "") @@ -187,7 +197,12 @@ class PDOpenAIGPTImage2AuthToken: duration = time.time() - start_time min_cost, max_cost = estimate_price_range_2(quality, num_images) token_cost = calculate_token_price_2(num_images) - size_note = resolve_gpt_image_2_size(size, custom_width, custom_height) + size_note = resolve_gpt_image_2_size( + size, + custom_width, + custom_height, + use_custom_size=use_custom_size, + ) exact_cost = calculate_exact_cost_gpt_image_2_from_usage(result) mode_label = "编辑" if is_edit_mode else "生成" diff --git a/py/_image_gpt_common.py b/py/_image_gpt_common.py index d7e4bce..1a7bc2b 100644 --- a/py/_image_gpt_common.py +++ b/py/_image_gpt_common.py @@ -275,7 +275,40 @@ def calculate_token_price_2(n): return cost * n -def resolve_gpt_image_2_size(size, custom_width, custom_height): +def _custom_size_enabled(use_custom_size): + if isinstance(use_custom_size, str): + return use_custom_size.strip().lower() in ("true", "1", "yes", "on") + return bool(use_custom_size) + + +def resolve_gpt_image_2_size( + size, + custom_width, + custom_height, + size_input=None, + width_input=None, + height_input=None, + use_custom_size=True, +): + if not _custom_size_enabled(use_custom_size): + return size + + if size_input is not None: + size_input = str(size_input).strip() + if size_input: + return size_input + + if width_input is not None and height_input is not None: + try: + width_input = int(width_input) + height_input = int(height_input) + except (TypeError, ValueError): + width_input = 0 + height_input = 0 + + if width_input > 0 and height_input > 0: + return f"{width_input}x{height_input}" + if custom_width > 0 and custom_height > 0: return f"{custom_width}x{custom_height}" return size @@ -326,8 +359,24 @@ def calculate_exact_cost_gpt_image_2_from_usage(result): } -def validate_gpt_image_2_size(size, custom_width=0, custom_height=0): - resolved_size = resolve_gpt_image_2_size(size, custom_width, custom_height) +def validate_gpt_image_2_size( + size, + custom_width=0, + custom_height=0, + size_input=None, + width_input=None, + height_input=None, + use_custom_size=True, +): + resolved_size = resolve_gpt_image_2_size( + size, + custom_width, + custom_height, + size_input=size_input, + width_input=width_input, + height_input=height_input, + use_custom_size=use_custom_size, + ) if resolved_size == "auto": return resolved_size