How to re size and crop an image using pillow library in python?

Description

To write a piece of python code to resize and crop an image using pillow library in python.

Input

JPG file. (Image file)

Output

  Resized and cropped image.

  Import pillow library.

  Load the image.

  Call the constructor (resize()) from pillow library.

  Define size of the image.

  Plot the resized image using matplot.lib.

  Call the crop constructor.

  Plot the cropped image.

.

#import pillow library
from PIL import Image
import matplotlib.pyplot as plt

#load image
image = Image.open(‘msd.jpg’)
print(“Original image size”)
print(image.size,”\n”)
print(“Original image\n”)
plt.imshow(image)
plt.show()

#Resize the image
print(“Resized image\n”)
new_image = image.resize((300, 200))
new_image.save(‘msd_400.jpg’)
plt.imshow(new_image)
plt.show()
print(“Resized image size\n”,new_image.size,”\n”)

#cropping the image
print(“Cropped image\n”)
new_image = image.crop((100, 75, 400, 400))
new_image.save(‘msd_400.jpg’)
plt.imshow(new_image)
plt.show()
print(“Cropped image size\n”,new_image.size)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit