MCPcopy Create free account
hub / github.com/Francis-Rings/StableAnimator / validate_and_convert_image

Function validate_and_convert_image

train_single.py:57–79  ·  view source on GitHub ↗
(image, target_size=(256, 256))

Source from the content-addressed store, hash-verified

55
56# i should make a utility function file
57def validate_and_convert_image(image, target_size=(256, 256)):
58 if image is None:
59 print("Encountered a None image")
60 return None
61
62 if isinstance(image, torch.Tensor):
63 # Convert PyTorch tensor to PIL Image
64 if image.ndim == 3 and image.shape[0] in [1, 3]: # Check for CxHxW format
65 if image.shape[0] == 1: # Convert single-channel grayscale to RGB
66 image = image.repeat(3, 1, 1)
67 image = image.mul(255).clamp(0, 255).byte().permute(1, 2, 0).cpu().numpy()
68 image = Image.fromarray(image)
69 else:
70 print(f"Invalid image tensor shape: {image.shape}")
71 return None
72 elif isinstance(image, Image.Image):
73 # Resize PIL Image
74 image = image.resize(target_size)
75 else:
76 print("Image is not a PIL Image or a PyTorch tensor")
77 return None
78
79 return image
80
81
82def create_image_grid(images, rows, cols, target_size=(256, 256)):

Callers 1

create_image_gridFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected