mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2025-05-06 20:09:06 +08:00
Merge ad8ab17b0c2f3f21adbf8396d79628d9395ddbf6 into 82bf9a373023f2317a7d30c28d9064ffe016e2c4
This commit is contained in:
commit
446fa49868
@ -105,6 +105,7 @@ options_templates.update(options_section(('upscaling', "Upscaling", "postprocess
|
|||||||
"DAT_tile_overlap": OptionInfo(8, "Tile overlap for DAT upscalers.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}).info("Low values = visible seam"),
|
"DAT_tile_overlap": OptionInfo(8, "Tile overlap for DAT upscalers.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}).info("Low values = visible seam"),
|
||||||
"upscaler_for_img2img": OptionInfo(None, "Upscaler for img2img", gr.Dropdown, lambda: {"choices": [x.name for x in shared.sd_upscalers]}),
|
"upscaler_for_img2img": OptionInfo(None, "Upscaler for img2img", gr.Dropdown, lambda: {"choices": [x.name for x in shared.sd_upscalers]}),
|
||||||
"set_scale_by_when_changing_upscaler": OptionInfo(False, "Automatically set the Scale by factor based on the name of the selected Upscaler."),
|
"set_scale_by_when_changing_upscaler": OptionInfo(False, "Automatically set the Scale by factor based on the name of the selected Upscaler."),
|
||||||
|
"upscaler_fast_prescale_threshold": OptionInfo(1.0, "Maximum threshold for fast pre-scaling of very small images.", gr.Slider, {"minimum": 1, "maximum": 8, "step": 0.01}).info("The maximum scale difference for performing fast Lanczos pre-scaling before the first upscaling step. This can prevent expensive additional steps if the source image is very small. 1 = disabled; 1.25-3.0 recommended"),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
options_templates.update(options_section(('face-restoration', "Face restoration", "postprocessing"), {
|
options_templates.update(options_section(('face-restoration', "Face restoration", "postprocessing"), {
|
||||||
|
@ -6,6 +6,7 @@ from PIL import Image
|
|||||||
|
|
||||||
import modules.shared
|
import modules.shared
|
||||||
from modules import modelloader, shared
|
from modules import modelloader, shared
|
||||||
|
import math
|
||||||
|
|
||||||
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
|
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
|
||||||
NEAREST = (Image.Resampling.NEAREST if hasattr(Image, 'Resampling') else Image.NEAREST)
|
NEAREST = (Image.Resampling.NEAREST if hasattr(Image, 'Resampling') else Image.NEAREST)
|
||||||
@ -56,6 +57,38 @@ class Upscaler:
|
|||||||
dest_w = int((img.width * scale) // 8 * 8)
|
dest_w = int((img.width * scale) // 8 * 8)
|
||||||
dest_h = int((img.height * scale) // 8 * 8)
|
dest_h = int((img.height * scale) // 8 * 8)
|
||||||
|
|
||||||
|
# Attempt a cheap resize of the source image, if it falls below the fixed scaling size of the upscaling model.
|
||||||
|
# We resize the image by the smallest amount necessary for the fixed scaling to meet the target dimensions.
|
||||||
|
prescale_threshold = modules.shared.opts.upscaler_fast_prescale_threshold
|
||||||
|
if prescale_threshold > 1 and self.name and self.name not in ["Nearest", "Lanczos"]:
|
||||||
|
# Get the matching upscaler
|
||||||
|
upscaler_data = next((x for x in self.scalers if x.data_path == selected_model), None)
|
||||||
|
|
||||||
|
if upscaler_data is not None:
|
||||||
|
upscaler_scale = upscaler_data.scale
|
||||||
|
if scale > upscaler_scale:
|
||||||
|
|
||||||
|
# Calculate the minimum intermediate dimensions.
|
||||||
|
min_intermediate_w = math.ceil(dest_w / upscaler_scale)
|
||||||
|
min_intermediate_h = math.ceil(dest_h / upscaler_scale)
|
||||||
|
|
||||||
|
# Preserve aspect ratio and make sure any adjustments don't drop us below the
|
||||||
|
# minimum scaling needed.
|
||||||
|
aspect_ratio = img.width / img.height
|
||||||
|
|
||||||
|
intermediate_w = max(min_intermediate_w, int(math.ceil(min_intermediate_h * aspect_ratio)))
|
||||||
|
intermediate_h = max(min_intermediate_h, int(math.ceil(min_intermediate_w / aspect_ratio)))
|
||||||
|
|
||||||
|
if intermediate_w / aspect_ratio > intermediate_h:
|
||||||
|
intermediate_w = int(math.ceil(intermediate_h * aspect_ratio))
|
||||||
|
else:
|
||||||
|
intermediate_h = int(math.ceil(intermediate_w / aspect_ratio))
|
||||||
|
|
||||||
|
scale_diff = max(intermediate_w / img.width, intermediate_h / img.height)
|
||||||
|
|
||||||
|
if scale_diff <= prescale_threshold:
|
||||||
|
img = img.resize((intermediate_w, intermediate_h), resample=LANCZOS)
|
||||||
|
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
if img.width >= dest_w and img.height >= dest_h and (i > 0 or scale != 1):
|
if img.width >= dest_w and img.height >= dest_h and (i > 0 or scale != 1):
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user