Search code examples
pythonpython-imaging-librarystable-diffusion

Resize any image to 512 x 512


I am trying to train a stable diffusion model, which receives 512 x 512 images as inputs.

I am downloading the bulk of images from the web, and they have multiple sizes and shapes, and so I need to preprocess them and convert to 512 x 512.

If I take this 2000 × 1434 image:

enter image description here

And I try to resize it, with:

from PIL import Image

# Open an image file
with Image.open("path/to/Image.jpg") as im:
    # Create a thumbnail of the image
    im.thumbnail((512, 512))
    # Save the thumbnail
    im.save("path/to/Image_resized.jpg")

I get this 512 × 367 image:

enter image description here

But I need 512 for both width and height, without distorting the image, like you can achieve on this website:

Birma

Any ideas on how I can achieve this conversion using python?


Solution

  • I dont think a resize of an image with another aspect ratio of 1:1 (512:512) is possible without distorting the image. You can resize the shorter dimension of the image to 512px and crop the large dimension to 512px.

    from PIL import Image
    
    # Open an image file
    with Image.open("image.jpg") as im:
        width, height = im.size
        if width < height:
            newWidth = 512
            newHeight = int(height / (width / 512))
            cropTop = (newHeight - 512) // 2
            cropBottom = cropTop + 512
            crop = (0, cropTop, 512, cropBottom)
        else:
            newHeight = 512
            newWidth = int(width / (height / 512))
            cropLeft = (newWidth - 512) // 2
            cropRight = cropLeft + 512
            crop = (cropLeft, 0, cropRight, 512)
        imResize = im.resize((newWidth, newHeight))
        imCrop = imResize.crop(crop)
        imCrop.save("image_resized_cropped.jpg")