CS 100 - Computer Literacy, Spring 2005
Lab 7
This assignment will have you take an image and create
a new image that will display the original picture multiple times.
For example, take the following image of George Washington.
This image shown in twice side-by-side/horizontally would be:
Duplicating an Image Side-by-Side
In order to create an image that will display an image twice
side-by-side,
we first need to create a blank canvas using makeEmptyPicture()
that will be be twice as wide as the original image but
have the same height.
So we will need to get the width and height from the original
image so we can determine the size needed for the duplicated
image.
Next we need a loop to access each pixel from the original image.
Each pixel must be put into the duplicated image twice, once
on the left side and once on the right side. We can use
getColor() to get the pixel's color information
from the original image and use setColor() twice to
put that color information into the duplicated image. The only
remaining question is:
"Where does the color information get
put in the duplicated image?"
To answer this question let us assume we are dealing with an
original image of 100x150 pixels. Hopefully by understanding
what is done with this example, we can determine how to deal
with this for any image.
- The original image will have
- its upper left corner at pixel 1,1
- its upper right corner at pixel 100,1
- its lower left corner at pixel 1,150
- its lower right corner at pixel 100,150
- The duplicated image image needs to have the same height
with twice the width of the original picture. So its size would be
200x150 pixels.
- The duplicated image can be thought of as having two different
sections. One section will hold the left side image. The other
section will hold the right side image.
- The left side image will have
- its upper left corner at pixel 1,1
- its upper right corner at pixel 100,1
- its lower left corner at pixel 1,150
- its lower right corner at pixel 100,150
- The right side image will have
- its upper left corner at pixel 101,1
- its upper right corner at pixel 200,1
- its lower left corner at pixel 101,150
- its lower right corner at pixel 200,150
- The color information from the pixel at position 1,1
(the upper left corner) in the original image would need to
be placed in both the left side image and the right side image
of the duplicated image. This would be at the pixels with
position of 1,1 and 101,1.
- The color information from the pixel at position 5,2
(close to the upper left corner) in the original image would need to
be placed in both the left side image and the right side image
of the duplicated image. This would be at the pixels with
position of 5,2 and 105,2.
- The color information from the pixel at position 98,142
(close to the lower right corner) in the original image would need to
be placed in both the left side image and the right side image
of the duplicated image. This would be at the pixels with
position of 98,142 and 198,142.
-
We have a pattern here!
The color information from the pixel at position X,Y
in the original image would need to
be placed in both the left side image and the right side image
of the duplicated image. This would be at the pixels with
position of X,Y and 100+X,Y.
-
Also note that the value of 100
is the width of the original picture! Thus for any picture,
the color information from the pixel at position X,Y
in the original image would need to
be placed in both the left side image and the right side image
of the duplicated image. This would be at the pixels with
position of X,Y and (width_of_the_original_image)+X,Y.
The following JES code shows how this is done:
def duplicateHorz():
#Allow the user to pick up an image file
file = pickAFile()
#now convert the image in JES format
pic = makePicture(file)
# get the size of the originalimage
origWidth = getWidth(pic)
origHeight = getHeight(pic)
# determine the size of new image
newWidth = origWidth * 2
newHeight = origHeight
#now creat a new image in JES format
new_pic = makeEmptyPicture(newWidth, newHeight)
# Loop for each pixel in the original image.
# Put that pixels color information in two positions
# in the new duplicated image.
for orig_pixel in getPixels(pic):
# get X and Y positions of pixel from new image
x = getX(orig_pixel)
y = getY(orig_pixel)
# get the color from original pixel
c = getColor(orig_pixel)
# set the color to the pixel in the left side to c
left_pixel = getPixel(new_pic, x, y)
setColor(left_pixel, c)
# set the color to the pixel in the right side to c
right_pixel = getPixel(new_pic, x+origWidth, y)
setColor(right_pixel, c)
#Now display the new image
show(new_pic)
# save the new image
writePictureTo(new_pic, pickAFile())
|
Program to duplicate an image within another image
Lab Assignment 7
Due: Monday 3/7/2005 by 12:00 noon
Modify the above code in two different ways.
- Write a function triplicateHorz() that will produce
three images side-by-side/horizontally. Doing this to the George
Washington image we would get:
- Write a function duplicateVert() that will produce
two images one on top of the other (i.e. vertically). Doing this to the George
Washington image we would get:
Both of these functions are to be written in the same python file.
You are to submit this function
by emailing it to the CS 100 course account at i100@cs.uic.edu.
Be sure to use a meaningful subject line on the email.
We would like to see any resulting images posted
on the
lab 7
page on CS 100 Swiki.
Try to use a small image when running your program. Some
images from our textbook can be found at
http://www.cs.uic.edu/~i100/images/other/index.html.