Skip to content

Instantly share code, notes, and snippets.

@zh4n7wm
Created September 1, 2020 09:40
Show Gist options
  • Save zh4n7wm/076efc1dc61ef8d70d09d698d4648a9b to your computer and use it in GitHub Desktop.
Save zh4n7wm/076efc1dc61ef8d70d09d698d4648a9b to your computer and use it in GitHub Desktop.
crop image

crop image

import os

from PIL import Image


def crop_image(filename, w_h_ratio=0.75):
    img = Image.open(filename)
    new_h = img.size[0] * w_h_ratio
    top = (img.size[1] - new_h) / 2
    bottom = new_h + top
    cropped = img.crop((0, top, img.size[0], bottom))
    name, ext = os.path.splitext(filename)
    ext = ext if ext else '.jpg'
    new_filename = f'{name}_crap{ext}'
    cropped.save(new_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment