pillow python crop 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: how to cut image python

im = Image.open('0.png').convert('L')
im = im.crop((1, 1, 98, 33))
im.save('_0.png')

Example 4: crop image python

import cv2
img = cv2.imread("lenna.png")
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)