Here is the equivalent tabular image from lab 7 solution.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Here are some important points:
Let's try to figure out how the colors are copied from original image to new one:
| Pixel in original image | Corresponding pixels in new image | ||||||
|---|---|---|---|---|---|---|---|
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
| So for each original pixel (x,y), the corresponding four pixels in the new image have following property: | |||||||
|
|
||||||
So we can modify the original program given as part of Lab 7 solution as follows
def makeCollage():
#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 * 2
#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 three 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 upper left copy to c
pixel1 = getPixel(new_pic, x, y)
setColor(pixel1, c)
# set the color to the pixel in the upper right copy to c
pixel2 = getPixel(new_pic, x+origWidth, y)
setColor(pixel2, c)
# set the color to the pixel in the lower left copy to c
pixel3 = getPixel(new_pic, x, y+origHeight)
setColor(pixel3, c)
# set the color to the pixel in the lower right copy to c
pixel2 = getPixel(new_pic, x+origWidth, y+origHeight)
setColor(pixel2, c)
#Now display the new image
show(new_pic)
# save the new image
writePictureTo(new_pic, pickAFile())
|
So this one will make four copies of the original image. But what we need is color modifications in the original image. So we will do following:
So instead of setting all the pixel colors to the same color, we will first compose these four colors and put them in four pixels. This is how modified program looks:
def makeCollage():
#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 * 2
#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 three 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
c1 = getColor(orig_pixel)
# get the color components original pixel
red = getRed(orig_pixel)
green = getGreen(orig_pixel)
blue = getBlue(orig_pixel)
# compute the average
avg = (red + green + blue) / 3
# make black and white color
c2 = makeColor(avg, avg, avg)
# make color without red component
c3 = makeColor(0, green, blue)
# make the negative color
c4 = makeColor(255-red, 255-green, 255-blue)
# set the color to the pixel in the upper left copy to c1
pixel1 = getPixel(new_pic, x, y)
setColor(pixel1, c1)
# set the color to the pixel in the upper right copy to c2
pixel2 = getPixel(new_pic, x+origWidth, y)
setColor(pixel2, c2)
# set the color to the pixel in the lower left copy to c3
pixel3 = getPixel(new_pic, x, y+origHeight)
setColor(pixel3, c3)
# set the color to the pixel in the lower right copy to c4
pixel2 = getPixel(new_pic, x+origWidth, y+origHeight)
setColor(pixel2, c4)
#Now display the new image
show(new_pic)
# save the new image
writePictureTo(new_pic, pickAFile())
|