For this program you\'d need to use the new Python Image Library (Pillow 3.0.0),
ID: 3669186 • Letter: F
Question
For this program you'd need to use the new Python Image Library (Pillow 3.0.0), compatible with Python 3.5.0 https://pypi.python.org/pypi/Pillow/3.0.0
Description of Program 1 Write and test a Python program that will do the following
• Open an image from a picture file.
• From the original image, create at least five more images applying the functions of the PIL Image module. You must apply the convert function using different arguments and the Image.blend function once.
• Use nested for loops to paste the (resized) images into a larger image, in the form of a collage. No nested loops, no points.
• The collage image must be of variable size--depending on the given arguments width and height. Fixed size, no points. Sample code below shows a list of images and then the call to the function create_collage with numeric arguments for width and height. The program must work for different values of the width and height of the collage.
listofimages = [im, one, two, three, four, five]
create_collage(450, 300, listofimages)
• All images must be resized to fit exactly into the newly created collage. Paste images into the collage.
Explanation / Answer
#!/usr/bin/python
import Image
# // here is a python image processing code using the Pillow
# // the stable version of pillow can be the wheel called:
# // Pillow-3.0.0-cp26-none-win32.whl (md5)
# // to mean it will work on a 32 bit processor
# // running windows 7 or higher
# //
# //
# // once we have the compiler and Integrated Development Environment (IDE) ready,
# // we can type in the following code
# //
# //
# read a file containing the picture
# open that picture - let us call it as the parent image
# create 5 or more derived images from the parent image
# must use the PIL Image module
# using the convert function
# make use of many modules like ?, ?, ?, ?, ?, etc
# Image.blend - use it atleast once - to get a blended image
# must use nested for loops - loop inside another loop
# like:
# for ( i = 1; i<10; i++)
# for ( j = i+1; j<20; j++)
# for ( k = j + 2; j< 30; j++)
# statements line 1
# statements line 2
# statements line 3
# statements line 4
# statements line 5
# end for k ultra inner
# end for j inner
# end for i outer
# open the file stream , assign to fp = file pointer
fp = open ("E:/Files/image.txt", "r")
print " Managed to open the file ", fp.name , " in the mode " , fp.mode
print " Present status of the file is " , fp.mode
# status of false means file is open, status of true means that the file is closed
# To know the present position of the file pointer inside the file
# seek and tell functions can be used
location = fp.tell();
print " We are at the location: ", location
# let us point to the begining of the file
location = fp.seek(0,0);
# The image class in PIL (Pillow 3.0.0) can be used:
from PIL import Image
picture1 = Image.open("PictureName.ppm")
# this has just given us an object of type Image.
#
from __future__import print_function
print(picture1.format, picture1.mode, picture1.size)
# we will use Red Green Blue (RGB) for real colors
# and CMYK will be used for pre-press images
# the loaded image from the file can be viewed by:
picture1.show()
# the loaded file can be converted to jpeg image type and then stored in to the collage
import os, sys
for pictureFile in sys.argv[1:]:
# picture file = in file
file, element = os.path.splitext(pictureFile)
jpegFile = file + ".jpg" # jpegFile = outfile
if pictureFile != jpegFile:
try:
Image.open(pictureFile).save(jpegFile)
except IOError:
print (" Sorry convertion failed, can not make the collage, please retry.....", pictureFile)
# open the nested for loops to make the thumbnails as part of the collage as well
resolution = (256, 256)
for pictureFile in sys.argv[1:]:
jpegFile = os.path.splitext(pictureFile)[0] + ".thumbnail"
if pictureFile != jpegFile:
try:
picture1 = Image.open(pictureFile)
picture1.thumbnail(resolution)
picture1.save(jpegFile, "JPEG")
export IOError:
print (" Sorry Thumbnail creation failed, and hence thumbnail images cannot be added to the collage, please retry.....", pictureFile)
# end inner nested for loop
# end outer nested for loop
#
collageWidth = input()
collageHeight = input()
ImageList = [picture1, picture2, picture3, picture4, picture5]
create_collage(collageWidth, collageHeight, ImageList)
# blending the images using the Image.blend function:
def reader( ): #{
nameOfFile1 = "image1.jpeg";
nameOfFile2 = "image2.jpeg";
picture1 = Image.open(nameOfFile1);
picture2 = Image.open(nameOfFile2);
TheBlendedPicture = Image.blend(picture1, picture2, 0.5);
TheBlendedPicture.show();
#
#
#
#
#
#
# after all work is done with the file stream, just close it and get rid of the Random Access Memory Buffer
fp.close()
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.