Resizing using Wand
This small manual is a draft of advanced resizing manual to be included to official Wand docs.
| .*.swp | |
| resized-*.jpg | |
| resized-*.png |
This small manual is a draft of advanced resizing manual to be included to official Wand docs.
| Wand >= 0.2.0 |
| 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) |