pillow crop image code example

Example 1: pillow python crop

im = Image.open('image.jpg') 
im = im.crop((left, top, width, height))

#       ├─input image width─┤
#   ┬   ┌───────────────────┐ ┬   ┬   
#   │   │                   │top  │   
# input │       ┌───────┐   │ ┴ height
# image │       │       │   │     │   
# height│       └───────┘   │     ┴   
#   │   │                   │  
#   ┴   └───────────────────┘   
#       ├─left─┤
#       ├─────width─────┤

Example 2: pil crop image

im = Image.open('image.jpg') 
im = im.crop((left, top, right, bottom))  # coordinates of the crop

Example 3: crop image python

from PIL import Image # import pillow library (can install with "pip install pillow")
im = Image.open('card.png')
im = im.crop( (1, 0, 826, 1125) ) # previously, image was 826 pixels wide, cropping to 825 pixels wide
im.save('card.png') # saves the image
# im.show() # opens the image

Example 4: pil resize image

im = Image.open('image.jpg')  
im = im.resize((w, h))