From 398a9bc0c6224c0f9cbb00c0b218634b464597a0 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Mon, 14 Nov 2022 17:41:02 +0000 Subject: [PATCH] fix incorrect bounding-box calculation in ImageResizer - Under some circumstances, the image resizer was fitting the wrong dimension to the user-provided bounding box when an init image provided. - Closes #1470. --- ldm/generate.py | 8 +------- ldm/invoke/image_util.py | 8 ++++---- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/ldm/generate.py b/ldm/generate.py index 9aebe314f3..5d4b7a736c 100644 --- a/ldm/generate.py +++ b/ldm/generate.py @@ -1072,14 +1072,8 @@ class Generate: print( f'>> image will be resized to fit inside a box {w}x{h} in size.' ) - if image.width > image.height: - h = None # by setting h to none, we tell InitImageResizer to fit into the width and calculate height - elif image.height > image.width: - w = None # ditto for w - else: - pass # note that InitImageResizer does the multiple of 64 truncation internally - image = InitImageResizer(image).resize(w, h) + image = InitImageResizer(image).resize(width=w, height=h) print( f'>> after adjusting image dimensions to be multiples of 64, init image is {image.width}x{image.height}' ) diff --git a/ldm/invoke/image_util.py b/ldm/invoke/image_util.py index 2ec7b55834..46cdf41184 100644 --- a/ldm/invoke/image_util.py +++ b/ldm/invoke/image_util.py @@ -31,10 +31,10 @@ class InitImageResizer(): elif not width: # width missing width = int(height*ar) - # rw and rh are the resizing width and height for the image - # they maintain the aspect ratio, but may not completelyl fill up - # the requested destination size - (rw,rh) = (width,int(width/ar)) if im.width>=im.height else (int(height*ar),height) + w_scale = width/im.width + h_scale = height/im.height + scale = min(w_scale,h_scale) + (rw,rh) = (int(scale*im.width),int(scale*im.height)) #round everything to multiples of 64 width,height,rw,rh = map(