| 18 | |
| 19 | |
| 20 | def preprocess_image(image): |
| 21 | from PIL import Image |
| 22 | |
| 23 | """Preprocess an input image |
| 24 | |
| 25 | Same as |
| 26 | https://github.com/huggingface/diffusers/blob/1138d63b519e37f0ce04e027b9f4a3261d27c628/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py#L44 |
| 27 | """ |
| 28 | w, h = image.size |
| 29 | w, h = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 |
| 30 | image = image.resize((w, h), resample=Image.LANCZOS) |
| 31 | image = np.array(image).astype(np.float32) / 255.0 |
| 32 | image = image[None].transpose(0, 3, 1, 2) |
| 33 | image = torch.from_numpy(image) |
| 34 | return 2.0 * image - 1.0 |
| 35 | |
| 36 | |
| 37 | @dataclass |