From a5e7b371d61bc063062b636236f1f37c264f4115 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Fri, 30 Sep 2022 10:38:48 +0300 Subject: [PATCH] fix the bug with broken rescaling in PR --- modules/images.py | 14 +++++++++++--- modules/upscaler.py | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/images.py b/modules/images.py index a6538dbe5..6430cfec2 100644 --- a/modules/images.py +++ b/modules/images.py @@ -219,9 +219,17 @@ def resize_image(resize_mode, im, width, height): if opts.upscaler_for_img2img is None or opts.upscaler_for_img2img == "None" or im.mode == 'L': return im.resize((w, h), resample=LANCZOS) - upscaler = [x for x in shared.sd_upscalers if x.name == opts.upscaler_for_img2img][0] - scale = w / im.width - return upscaler.scaler.upscale(im, scale) + upscalers = [x for x in shared.sd_upscalers if x.name == opts.upscaler_for_img2img] + assert len(upscalers) > 0, f"could not find upscaler named {opts.upscaler_for_img2img}" + + upscaler = upscalers[0] + scale = max(w / im.width, h / im.height) + upscaled = upscaler.scaler.upscale(im, scale) + + if upscaled.width != w or upscaled.height != h: + upscaled = im.resize((w, h), resample=LANCZOS) + + return upscaled if resize_mode == 0: res = resize(im, width, height) diff --git a/modules/upscaler.py b/modules/upscaler.py index 7b51844ff..d9d7c5e2a 100644 --- a/modules/upscaler.py +++ b/modules/upscaler.py @@ -60,7 +60,7 @@ class Upscaler: break img = self.do_upscale(img, selected_model) if img.width != dest_w or img.height != dest_h: - img = img.resize((dest_w, dest_h), resample=LANCZOS) + img = img.resize((int(dest_w), int(dest_h)), resample=LANCZOS) return img