Python - Multiple Image Copies



Description

Most of us might be experiencing how passport size image is created. Photographer initially takes the single snap and will the snap to his system and using some image editor tool he will generate multiple copies of image to a single sheet.

We are rotate the images with 90,180,270 degrees either in clockwise or anticlockwise direction.

We can also rotate the images with Python combing with Python Image Library.Other than 90,180,270 degrees we have the flexibility of rotating as per our need.

We can acheive the same with Python combining with Image Library.

Let us take the below image and will try to copying multiple images to a same sheet.

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.
from PIL import Image
#opens an image:
im = Image.open("view.jpg")

Creates a new empty image, RGB mode, and size 400 by 400.
new_im = Image.new('RGB', (400,400))br>

Resize the image into thumbnails
im.thumbnail((100,100))

Iterate through a 4 by 4 grid with 100 spacing, to place image
for i in xrange(0,500,100):
for j in xrange(0,500,100):
#paste the image at location i,j:
new_im.paste(im, (i,j))



Image can be displayed as below
new_im.show()

Return value

The new image with multiple copies of the original gets saved to local directory and is also displayed.

Examples

Here is the complete program to make the image into multiple copies(passport size copies).
from PIL import Image
#opens an image:
im = Image.open("single_image.jpg")
#creates a new empty image, RGB mode, and size 400 by 400.
new_im = Image.new('RGB', (400,400))
#Here I resize my opened image, so it is no bigger than 100,100
im.thumbnail((100,100))
#Iterate through a 4 by 4 grid with 100 spacing, to place image
for i in xrange(0,500,100):
for j in xrange(0,500,100):
#paste the image at location i,j:
new_im.paste(im, (i,j))
new_im.show()
new_im.save("multi_image.jpg")


Before
single_image
After
multi_image

The brightness can be changed from frame to frame if we modify the above program slightly.
from PIL import Image
#opens an image:
im = Image.open("single_image.jpg")
#creates a new empty image, RGB mode, and size 400 by 400.
new_im = Image.new('RGB', (400,400))
#Here I resize my opened image, so it is no bigger than 100,100
im.thumbnail((100,100))
#Iterate through a 4 by 4 grid with 100 spacing, to place image
for i in xrange(0,500,100):
for j in xrange(0,500,100):
#I change brightness of the images, just to emphasise they are unique copies.
im=Image.eval(im,lambda x: x+(i+j)/30)
#paste the image at location i,j:
new_im.paste(im, (i,j))
new_im.show()
new_im.save("multi_image_brightness.jpg")

Before
single_image
After
multi_image

Advertisements