pythonmagickwandwand

Create square thumbnails with Python + MagickWand


How can I create square thumbnails with Python and Wand? I'm trying to make square thumbnails from source images of any size. It's important that the thumbnail have the same aspect ratio as the original, cropping is ok, and it should fill the sapce of the thumbnail.


Solution

  • The following crop_center() function makes the given image square.

    from __future__ import division
    
    from wand.image import Image
    
    
    def crop_center(image):
        dst_landscape = 1 > image.width / image.height
        wh = image.width if dst_landscape else image.height
        image.crop(
            left=int((image.width - wh) / 2),
            top=int((image.height - wh) / 2),
            width=int(wh),
            height=int(wh)
        )
    

    First you need to make the image square, and then you can resize() the square smaller.