Pillow enables you to paste an image onto another one. Some example use cases where this could be useful is in the protection of publicly available images by adding watermarks on them, the branding of images by adding a company logo and in any other case where there is a need to merge two images.
Pasting is done with the paste()
function. This modifies the Image
object in place, unlike the other processing functions we've looked at so far that return a new Image
object. Because of this, we'll first make a copy our demo image before performing the paste, so that we can continue with the other examples with an unmodified image.
The second argument can be a 2-tuple (specifying the top left corner), or a 4-tuple (left, upper, right, lower) – in this case the size of pasted image must match the size of this box region, or None which is equivalent to (0, 0).
# Improting Image class from PIL module
from PIL import Image
#Relative Path
#Image on which we want to paste
img=Image.open("nature.jpg")
#Relative Path
#Image on which we want to paste
img1=Image.open("logo.jpg")
img.paste(img1,(100,75))
img.show()
Here is the sample output is
Original Image is
Pasted Image is
This will return a histogram of the image as a list of pixel counts, one for each pixel in the image. (A histogram of an image is a graphical representation of the tonal distribution in a digital image. It contains what all the brightness values contained in an image are. It plots the number of pixels for each brightness value. It helps in doing the exposure settings.)
from PIL import Image
#Relative Path
img =Image.open("nature.jpg")
#Getting histogram of image
print(img.histogram())
Here is the Output
Original Image
And the Histogram of above image is
This feature gives us the mirror image of an image
# Improting Image class from PIL module
from PIL import Image
#Relative Path
img =Image.open("moon.jpg")
img.show() # Display Original Image
#transposing image
transposed_img =img.transpose(Image.FLIP_LEFT_RIGHT)
#Save transposed image
transposed_img.save("transposed.jpg")
transposed_img.show() # Displayed Transposed Image(Mirror Image)
Output is
Original Image
Transposed Image i.e. Mirror image of original image is
Splitting an image in RGB mode, creates three new images each containing a copy of the original individual bands.
# Improting Image class from PIL module
from PIL import Image
#Relative Path
img =Image.open("nature.jpg")
#splitting the image and printing it
print(img.split())
Output is
Origina Image
Individual bands are
(<PIL.Image.Image image mode=L size=259x194 at 0x7FA61AA82A58>,
<PIL.Image.Image image mode=L size=259x194 at 0x7FA61AA826D8>,
<PIL.Image.Image image mode=L size=259x194 at 0x7FA61AA5D978>)
This method creates a thumbnail of the image that is opened. It does not return a new image object, it makes in-place modification to the currently opened image object itself. If you do not want to change the original image object, create a copy and then apply this method. This method also evaluates the appropriate to maintain the aspect ratio of the image according to the size passed.
# Improting Image class from PIL module
from PIL import Image
#Relative Path
img =Image.open("picture.jpg")
#In-place modification
img.thumbnail((200, 200))
img.save("thumb.jpg")
Output is
Original Image
Thumbnail Image is
Write a program to perform following operations on Image
a) Pasting an image on another Image
b) Getting a Histogram of an Image
c) Transposing an Image
d) Spliting an Image into Individual Bands
e) Creating a Thumbnail