| 228 | |
| 229 | |
| 230 | def prepare_image(image): |
| 231 | if isinstance(image, torch.Tensor): |
| 232 | # Batch single image |
| 233 | if image.ndim == 3: |
| 234 | image = image.unsqueeze(0) |
| 235 | |
| 236 | image = image.to(dtype=torch.float32) |
| 237 | else: |
| 238 | # preprocess image |
| 239 | if isinstance(image, (PIL.Image.Image, np.ndarray)): |
| 240 | image = [image] |
| 241 | |
| 242 | if isinstance(image, list) and isinstance(image[0], PIL.Image.Image): |
| 243 | image = [np.array(i.convert("RGB"))[None, :] for i in image] |
| 244 | image = np.concatenate(image, axis=0) |
| 245 | elif isinstance(image, list) and isinstance(image[0], np.ndarray): |
| 246 | image = np.concatenate([i[None, :] for i in image], axis=0) |
| 247 | |
| 248 | image = image.transpose(0, 3, 1, 2) |
| 249 | image = torch.from_numpy(image).to(dtype=torch.float32) / 127.5 - 1.0 |
| 250 | |
| 251 | return image |
| 252 | |
| 253 | |
| 254 | class RerenderAVideoPipeline(StableDiffusionControlNetImg2ImgPipeline): |