From 3d85b6e841aa2f6c0f6e690ca69b6472d3c3fd93 Mon Sep 17 00:00:00 2001 From: justumen Date: Thu, 11 Jul 2024 11:10:19 +0200 Subject: [PATCH] fix resize image --- .gitignore | 1 + __pycache__/resize_image.cpython-312.pyc | Bin 2007 -> 2505 bytes resize_image.py | 55 ++++++++++++++--------- 3 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/__pycache__/resize_image.cpython-312.pyc b/__pycache__/resize_image.cpython-312.pyc index f7836e1e8144e1100d833371ca4a9f85eb6f4969..7f019dac450fde492ed904300340ac2740b1fbd4 100644 GIT binary patch delta 1291 zcmdT^PfXKb6#u%e-G7F3f*b1w$j~hcvT4i|q9!WDKP3K%Atta?R>lU~nrbA| zC5$u?=VjwIgE4q>$&y_)YQm^y=8K0NxOg+lUB7~5nwWU@C4KMry}#{y?`zHYhH83i zFlYgy_l2cNuli-vafOjYM;txhA0xg3gd2)e!m`4=ADcc50ACG&6p>V(2V;2vQl#)w zVW&yIVx9mXq?0oJNpZc~LvK9625rAZPYbHgg5q(=6`z@t?qh#O=}$N~htvgITpJEXL6 zde3N+Q9~xRvgagr1EB77SpT2qV4=>&m&j=C7x3@SzwNi?bb++&wI`Drzw$Y;H$=@p z+O^ktsa;NTn}tMVHq0epr!wLXhIwgS zp=Oegh8dn`5|CtK@dU?0MT8SD!kH!+j$b6N7j#9Dph=ID-@c zsFSTo(FL{g3B?&?e20loOvR*2)6pR;67KX(wCDFhojS1MUG@s6YgBiY>K3VEtM_U>gVmnF@}0_5X-e!F5?hBeLv>4MzBkufbgZ`)?PW*hXi0Na zvO~*DU_lDjr sJSE??cM=|+lIDeeNob|`b_v@hc1U=jI)V)dFH}}TKj!%c5ZiJ638Wn-uK)l5 delta 825 zcmYjNOK1~87~Y+ICY#+RT}|60O>5hP25eE#*0czXdQniQf~R7M-Azo|JUY8Y8&c|} zkQA|xImAMtxd?%t1VKc>v*;lqhsfd~f|uSzy?79vNsR3e^Ue3azW<+juQs}r53+2h zuo-us#+JDk$_3yOu*=}{;|8Fp5~a~-7EFO0hl;=}fW>PR1y7@!>>xO#(FPc}%q))L zkSEV-ltyiUHkv$9C6;XIw|NI+OdqCetSO@+>l-)8$d)CY9d>+UVOHY~$x7IgD*6Ic znn%A`A9~KZI4;j6d(a!UPja*?a7`~-1}eJ8hzxHE=oiy3@rQIf+wPZ+*YlCu^?zB7 z&PjMN!5sLnHP~UtcV6hIt%-RtM&F@A{H=H^-^|P$kxh^A-%*gj(H{&Um&Gh(S1c~Q zlFF`HY+6gCERfDxg0Z}!>$y?g64Ls^=%a)INxNl>!AvR&VH6iHEG4oQA2rO?td1`6 zp>@v8z=a1EZ|Z3y14A~8HT0zzB=3hZ0fk7=35!Xj;+BMA9_mISr)!pS!kNeOyHEOU zJ;R2(2RjB`MDP@bT<`6c$aUlw-0KY-c}z7O&h3k(@J<*_H`I}uI#O4IHAk>Of8!~K zyfsmr*p6&YeRg)2>^t^n=aF29G$ltXKD#;n$?4iXvpc&F4(xmOYWGmhIb4W*Rn)Ca zF|&XFAiftbM=P$%)w=&&!#`Q`Pre23#MffoAE^&3R|*Us(&H&5car5m#a$kF8F)4J zVyy0-DM&wg%Hu=RqTj1v>K{Is+ncM5*Zad&(bIGeRl5RJF@T 3: - i = np.squeeze(i) - # Ensure the image is 3D (height, width, channels) - if i.ndim == 2: - i = i[:, :, np.newaxis] # Add a channel dimension if it's missing - - img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8)) - - # Resize the image - img_resized = img.resize((width, height), Image.LANCZOS) - - # Convert the PIL image back to numpy array - img_resized_np = np.array(img_resized).astype(np.float32) / 255.0 - - # Assuming ComfyUI format needs the image back in tensor format, convert it back - img_resized_tensor = torch.tensor(img_resized_np) - - return (img_resized_tensor, ) + # Ensure the input image is on CPU and convert to numpy array + image_np = image.cpu().numpy() + + # Check if the image is in the format [batch, height, width, channel] + if image_np.ndim == 4: + # If so, we'll process each image in the batch + resized_images = [] + for img in image_np: + # Convert to PIL Image + pil_img = Image.fromarray((img * 255).astype(np.uint8)) + # Resize + resized_pil = pil_img.resize((width, height), Image.LANCZOS) + # Convert back to numpy and normalize + resized_np = np.array(resized_pil).astype(np.float32) / 255.0 + resized_images.append(resized_np) + + # Stack the resized images back into a batch + resized_batch = np.stack(resized_images) + # Convert to torch tensor + return (torch.from_numpy(resized_batch),) + else: + # If it's a single image, process it directly + # Convert to PIL Image + pil_img = Image.fromarray((image_np * 255).astype(np.uint8)) + # Resize + resized_pil = pil_img.resize((width, height), Image.LANCZOS) + # Convert back to numpy and normalize + resized_np = np.array(resized_pil).astype(np.float32) / 255.0 + # Add batch dimension if it was originally present + if image.dim() == 4: + resized_np = np.expand_dims(resized_np, axis=0) + # Convert to torch tensor + return (torch.from_numpy(resized_np),) \ No newline at end of file