Upload webp_test.py
Browse files- webp_test.py +54 -0
webp_test.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
class LoadWebPAnimation:
|
| 7 |
+
@classmethod
|
| 8 |
+
def INPUT_TYPES(cls):
|
| 9 |
+
return {
|
| 10 |
+
"required": {
|
| 11 |
+
"path": ("STRING", {"default": ""}),
|
| 12 |
+
}
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
RETURN_TYPES = ("IMAGE",)
|
| 16 |
+
FUNCTION = "load_animation"
|
| 17 |
+
|
| 18 |
+
def load_animation(self, path):
|
| 19 |
+
# Check if the file exists
|
| 20 |
+
if not os.path.exists(path):
|
| 21 |
+
raise FileNotFoundError(f"File not found: {path}")
|
| 22 |
+
|
| 23 |
+
# Open the .webp file
|
| 24 |
+
with Image.open(path) as img:
|
| 25 |
+
# Check if it's an animation
|
| 26 |
+
if hasattr(img, 'is_animated') and img.is_animated:
|
| 27 |
+
frames = []
|
| 28 |
+
# Extract each frame
|
| 29 |
+
for i in range(img.n_frames):
|
| 30 |
+
img.seek(i)
|
| 31 |
+
frame = img.copy().convert('RGB') # Ensure 3 channels (RGB)
|
| 32 |
+
frames.append(np.array(frame))
|
| 33 |
+
batch = np.stack(frames, axis=0) # Shape: (num_frames, height, width, 3)
|
| 34 |
+
else:
|
| 35 |
+
# Single image case
|
| 36 |
+
frame = img.convert('RGB')
|
| 37 |
+
batch = np.expand_dims(np.array(frame), axis=0) # Shape: (1, height, width, 3)
|
| 38 |
+
|
| 39 |
+
# Convert to float32 and normalize to [0, 1]
|
| 40 |
+
batch = batch.astype(np.float32) / 255.0
|
| 41 |
+
|
| 42 |
+
# Convert to PyTorch tensor without permuting
|
| 43 |
+
batch = torch.from_numpy(batch) # Shape: (num_frames, height, width, 3)
|
| 44 |
+
|
| 45 |
+
return (batch,) # Return as a tuple
|
| 46 |
+
|
| 47 |
+
# Register the node with ComfyUI
|
| 48 |
+
NODE_CLASS_MAPPINGS = {
|
| 49 |
+
"LoadWebPAnimation": LoadWebPAnimation
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 53 |
+
"LoadWebPAnimation": "Load WebP Animation"
|
| 54 |
+
}
|