def crop_center(pil_img, crop_width, crop_height): img_width, img_height = pil_img.size return pil_img.crop(((img_width - crop_width) // 2, (img_height - crop_height) // 2, (img_width + crop_width) // 2, (img_height + crop_height) // 2)) im_new = crop_center(im, 300, 150) im_new.save('img.jpg', quality=95)
Here is what the above code is Doing:
1. We’re using the crop() method to crop the image.
2. We’re passing in the top left and bottom right coordinates of the crop.
3. We’re using the // operator to divide the width and height by 2.
4. We’re using the quality=95 argument to save the image with a quality of 95.