- PIL Home
- Python Image Library Intro
- Python Image Library Setup
- Python Image Library Basics
- Python Image Library Hierarchy
- Python Image Library Modules
- Python ImageColor Module
- Python ImageDraw Module
- Python ImageStat Module
- Python ImageTK Module
- Python ImageFile Module
- Python ImageFilter Module
- Python ImageGrab Module
- Python ImageMath Module
- Python - Loading Image
- Python - Rotating Image
- Python - Blurring Image
- Python - Converting Image
- Python - Cropping Image
- Python - Image Thumbnails
- Python - Text on Images
- Python - Multiple Image Copies
- Python - Saving Image As PDF
- Python - Saving Screenshot
- Python - Resizing Image
- Python - Adding Watermark
- Python Image Library Resources
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
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.
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")
#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))
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")
#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
After
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")
#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
After
Advertisements