Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

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.

Process

  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.

Sample Code

#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)

Screenshots
re size and crop an image using pillow library in python
PIL import Image
Plot the resized image using matplot.lib.