mirror of
https://github.com/justUmen/Bjornulf_custom_nodes.git
synced 2026-03-26 06:45:44 -03:00
fix resize image
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
__pycache__
|
||||||
Binary file not shown.
@@ -19,25 +19,36 @@ class ResizeImage:
|
|||||||
CATEGORY = "Bjornulf"
|
CATEGORY = "Bjornulf"
|
||||||
|
|
||||||
def resize_image(self, image, width=256, height=256):
|
def resize_image(self, image, width=256, height=256):
|
||||||
# Convert the image from ComfyUI format to PIL Image
|
# Ensure the input image is on CPU and convert to numpy array
|
||||||
i = 255. * image.cpu().numpy()
|
image_np = image.cpu().numpy()
|
||||||
|
|
||||||
# Reshape the image if it's not in the expected format, remove any leading dimensions of size 1
|
# Check if the image is in the format [batch, height, width, channel]
|
||||||
if i.ndim > 3:
|
if image_np.ndim == 4:
|
||||||
i = np.squeeze(i)
|
# If so, we'll process each image in the batch
|
||||||
# Ensure the image is 3D (height, width, channels)
|
resized_images = []
|
||||||
if i.ndim == 2:
|
for img in image_np:
|
||||||
i = i[:, :, np.newaxis] # Add a channel dimension if it's missing
|
# Convert to PIL Image
|
||||||
|
pil_img = Image.fromarray((img * 255).astype(np.uint8))
|
||||||
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
|
# Resize
|
||||||
|
resized_pil = pil_img.resize((width, height), Image.LANCZOS)
|
||||||
# Resize the image
|
# Convert back to numpy and normalize
|
||||||
img_resized = img.resize((width, height), Image.LANCZOS)
|
resized_np = np.array(resized_pil).astype(np.float32) / 255.0
|
||||||
|
resized_images.append(resized_np)
|
||||||
# Convert the PIL image back to numpy array
|
|
||||||
img_resized_np = np.array(img_resized).astype(np.float32) / 255.0
|
# Stack the resized images back into a batch
|
||||||
|
resized_batch = np.stack(resized_images)
|
||||||
# Assuming ComfyUI format needs the image back in tensor format, convert it back
|
# Convert to torch tensor
|
||||||
img_resized_tensor = torch.tensor(img_resized_np)
|
return (torch.from_numpy(resized_batch),)
|
||||||
|
else:
|
||||||
return (img_resized_tensor, )
|
# 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),)
|
||||||
Reference in New Issue
Block a user