Skip to content

Instantly share code, notes, and snippets.

@dahlia
Created August 9, 2012 08:13
Show Gist options
  • Save dahlia/3302248 to your computer and use it in GitHub Desktop.
Save dahlia/3302248 to your computer and use it in GitHub Desktop.
Resizing using Wand
.*.swp
resized-*.jpg
resized-*.png

Resizing using Wand

This small manual is a draft of advanced resizing manual to be included to official Wand docs.

from __future__ import division
from wand.image import Image
def resize(image, (width, height), filter='triangle'):
portrait = image.width < image.height
if portrait:
image_size = image.size
keywords = 'top', 'left', 'height', 'width'
else:
image_size = image.height, image.width
keywords = 'left', 'top', 'width', 'height'
crop_size = int(image_size[0] * height / width)
args = (int((image_size[1] - crop_size) / 2), 0, crop_size, image_size[0])
image.crop(**dict(zip(keywords, args)))
image.resize(width, height, filter)
if __name__ == '__main__':
files = [
'portrait-378x483.png',
'portrait-500x632.jpg',
'landscape-500x333.png',
'landscape-942x612.jpg'
]
for filename in files:
with open(filename) as image_file:
with Image(file=image_file) as image:
resize(image, (400, 400), 'lanczos')
image.save(filename='resized-' + filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment