Python - Cropping Image



Description

Cropping is a process used to improve the photo or image by cutting down the part of image leaving behind the unwanted area.Cropping is available as a tool in almost all the image editing tools.

crop method returns a tuple with copy of a rectangular region from the current image.The tuple is defined with left,upper,right,lower pixel coordinates
For Cropping the image we need to import Image module.

Let us take the below image and will try to rotate the image.

rose

First import the Image module and load the image with open method in Image.Make sure to give the complete path of the image where it has been placed.The image name can only be given if the image resides in the same program directory.
# Import Pillow:
from PIL import Image
# Load the original image:
img = Image.open("before_crop.jpg")

Apply the crop method in Image module to rotate the image in counterclockwise direction as below.
img2 = img.crop((0, 0, 300, 300))

Save the blurred file with save method from Image module.
img2.save("after_crop_2.jpg")


Syntax

Following is the syntax of functions been used.
# Import Pillow:
from PIL import Image,ImageFilter
# Load the original image:
object = Image.open(image)
object = object.crop((x1,y1,x2,y2))
object.save(image)

Parameters

For the crop method x1,y1,x2,y2 are the coordinates from where to crop the image.
x1,y1 - indicates the left upper coordinates x2,y2 - indicates the right lower corordinates Note:If image is in same directory where your python program resides, you can give the image name itself. If the image resides in some other directory, you are supposed to define the complete path of the image.

Return value

The new image which has been cropped will be saved to the local directory with the save method.

Examples

Here is the complete program to crop the image at top left corner and save the image.
# Import Pillow:
from PIL import Image
# Load the original image:
img = Image.open("before_crop.jpg")
##crop method
img2 = img.crop((0, 0, 300, 300))
img2.save("after_crop_2.jpg")

Before
flower
After
flower

Now cropping the image at the bottom right corner.
# Import Pillow:
from PIL import Image
# Load the original image:
img = Image.open("before_crop.jpg")
#getting width and height
width = img.size[0]
height = img.size[1]
# syntax : crop(x1,y1,x2,y2)
img3 = img.crop((width-450,height-450,width,height))
img3.save("after_crop_2.jpg"))

scenry

Cropping the center part of the image
# Import Pillow:
from PIL import Image
# Load the original image:
img = Image.open("before_crop.jpg")
half_the_width = img.size[0] / 2
half_the_height = img.size[1] / 2
img4 = img.crop(
(
half_the_width - 100,
half_the_height -100,
half_the_width + 100,
half_the_height + 100
)
)
img4.save("after_crop_4.jpg")

scenry

Converting the rectangular coordinates of image to a square.
# Import Pillow:
from PIL import Image
# Load the original image:
img = Image.open("before_crop.jpg")
longer_side = max(img.size)
horizontal_padding = (longer_side - img.size[0]) / 2
vertical_padding = (longer_side - img.size[1]) / 2
img5 = img.crop(
(
-horizontal_padding,
-vertical_padding,
img.size[0] + horizontal_padding,
img.size[1] + vertical_padding
)
)
img5.save("after_crop_5.jpg")

scenry

Advertisements