Python Pillow - Cutting and Pasting Images



Cutting Images

Pillow (Python Imaging Library) allows us to extract a rectangular region from an image. The extracted region of the image is also known as a bounding box from an image. In The crop() method of the Image module creates a new image that represents the specified region of the original image. This method allows us to specify the coordinates of the bounding box for the region we want to crop.

Here is the syntax and usage of the 'crop()' method in Pillow −

Image.crop(box)

Where,

  • box − This is a tuple specifying the rectangular region we want to extract. The box parameter should be a tuple of four values: (left, upper, right, lower).

    • left is the x-coordinate of the left edge of the bounding box.

    • upper is the y-coordinate of the upper edge of the bounding box.

    • right is the x-coordinate of the right edge of the bounding box.

    • lower is the y-coordinate of the lower edge of the bounding box.

Example

In this example we are cropping a rectangular portion as per our requirement using the crop() method of the Image module.

from PIL import Image

#Open the image
image = Image.open("Images/book.jpg")

#Define the bounding box for cropping
box = (100, 100, 200, 200)  
#(left, upper, right, lower)

#Crop the image using the defined bounding box
cropped_image = image.crop(box)

#Save or display the cropped image
cropped_image.save("output Image/cropped_image.jpg")
cropped_image.show()

Image to be cropped

book

Output

cropped image

Example

Here this is another example for cropping a rectangular part of the image by using the crop() method.

from PIL import Image

#Open the image
image = Image.open("Images/rose.jpg")

#Define the bounding box for cropping
box = (10, 10, 200, 200)  
#(left, upper, right, lower)

#Crop the image using the defined bounding box
cropped_image = image.crop(box)

#Save or display the cropped image
cropped_image.save("output Image/cropped_image.jpg")
cropped_image.show()

Input Image

flower

Output

cropped_image

Pasting Images

Pasting images using Python Pillow allows us to extract a region of interest from one image and paste it onto another. This process is useful for tasks like image cropping, object extraction and compositing.

The paste() method in Pillow (Python Imaging Library) allows us to paste one image onto another at a specified position. It's a commonly used method for compositing images, adding watermarks or overlaying one image on top of another.

The below is the syntax and parameters of the paste() function −

PIL.Image.paste(im, box, mask=None)
  • im − This is the source image i.e., the image we want to paste onto the current image.

  • box − This parameter specifies the position where we want to paste the source image. It can be a tuple of coordinates '(left, upper, right, lower)'. The source image will be pasted inside the bounding box defined by these coordinates.

  • mask (optional) − If this parameter provided then it can be an image that defines a transparency mask. The pasted image will be masked according to the transparency values in the mask image.

Example

Here is an example of how to use the paste() method to paste one image onto another.

from PIL import Image

#Open the background image
background = Image.open("Images/bw.png")

#Open the image to be pasted
image_to_paste = Image.open("Images/tutorialspoint.png")

#Define the position where the image should be pasted
position = (100, 100)

#Paste the image onto the background
background.paste(image_to_paste, position)

#Save the modified image
background.save("OutputImages/paste1.jpg")
background.show()

Image to be used

black white tutorialpoint

Output

paste1
Advertisements