You are on page 1of 18

Rose-colored glasses (lab #3)

The function makes an image look pink. To do this I reduced the green to 10%
and the blue to 80%.
Function
def roseColoredGlasses(pic):
pixels = getPixels(pic)
for p in pixels:
g = getGreen(p)
setGreen(p, g * 0.10)
b = getBlue(p)
setBlue(p, b * .80)
return(pic)

#Call Function
filename = pickAFile()
pic = makePicture(filename)
roseColoredGlasses(pic)
repaint(pic)
writePictureTo(pic, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Image
Portfolio\\roseColored.jpg")
Negative (lab #3)

The function creates the negative of an original image. This means we had to
use the opposite value for red, green, and blue.
Function
def makeNegative(pic):
pixels = getPixels(pic)
for p in pixels:
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
negativeColor = makeColor(255 - r, 255 - g, 255 - b)
setColor(p, negativeColor)

#Call Function
filename = pickAFile()
pic = makePicture(filename)
makeNegative(pic)
repaint(pic)
writePictureTo(pic, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Image
Portfolio\\negative.jpg")
Better Black and White (lab #3)

With the better black and white function, we simulated a black and white
image by converting the color image to grayscale. Gray colors occur when
RGB values for a pixel are all the same. We had to get the luminance of an
image. Luminance is roughly the average of the R, G, and B values for a pixel.
Function
def BnW(pic):
pixels = getPixels(pic)
for p in pixels:
avg = (getRed(p) + getGreen(p) + getBlue(p)) / 3
grayColor = makeColor(avg, avg, avg)
setColor(p, grayColor)

#Call Function
filename = pickAFile()
pic = makePicture(filename)
BnW(pic)
repaint(pic)
writePictureTo(pic, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Image
Portfolio\\BnW.jpg")
Bottom To Top Mirror (lab #4)

In this function, we copied the pixels from the bottom half of the image to the
top half of the image. We used for loops to get the x and y coordinates of the
image and used setColor to paste the pixels.
Function
def mirrorBottomToTop():
width = getWidth(pic)
height = getHeight(pic)
for x in range (0, width):
for y in range(0, height/2):
oldpixels = getPixel(pic, x, y)
newPixels = getPixel(pic, x, height-y-1)
setColor(oldpixels, getColor(newPixels))

#mirrorBottomToTop()
filename = pickAFile()
pic = makePicture(filename)
mirrorBottomToTop()
repaint(pic)
writePictureTo(pic, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Image
Portfolio\\bottomToTop.jpg")
Shrink (lab #4)

This function reduces the size of an image by 50%.


Function
def shrink(pic):
width = getWidth(pic)
height = getHeight(pic)
canvas = makeEmptyPicture(width/2, height/2)
for x in range (0, width):
for y in range (0, height):
color = getColor(getPixel(pic, x, y))
setColor(getPixel(canvas, x/2, y/2), color)
show(canvas)
return canvas

#call function
filename = pickAFile()
pic = makePicture(filename)
shrink(pic)
repaint(pic)
writePictureTo(pic, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Image
Portfolio\\shrink.jpg")
Collage (lab #5)

This collage program was a collection of techniques from previous labs. The
tricky part here was rearranging images by their coordinates. I changed the
size of my images to fit a specific width and height.
Function
def makeCollage():
target = makeEmptyPicture(612, 792, white)
pic1 = makePicture(pickAFile())
pic2 = makePicture(pickAFile())
pic3 = makePicture(pickAFile())
pic4 = makePicture(pickAFile())
pic5 = makePicture(pickAFile())
pic6 = makePicture(pickAFile())
pic7 = makePicture(pickAFile())
pic8 = makePicture(pickAFile())
pic9 = makePicture(pickAFile())

#P
target = pyCopy(pic1, target, 0, 0)
#Y
target = pyCopy(pic2, target, 200, 0)
#T
pic3 = lightenUp(pic3, blue)
target = pyCopy(pic3, target, 400, 0)
#H
target = pyCopy(pic4, target, 0, 200)
#O
target = pyCopy(pic5, target, 200, 200)
#N
target = pyCopy(pic6, target, 400, 200)
#rose colored beaker
pic7 = roseColoredGlasses(pic7)
target = pyCopy(pic7, target, 0, 450)
#Peaches
pic8 = betterBnW(pic8)
target = pyCopy(pic8, target, 200, 450)
#otter
target = pyCopy(pic9, target, 400, 500)
#image placement
def pyCopy(source, target, targetX, targetY):
for x in range (0, getWidth(source)):
for y in range (0, getHeight(source)):
px = getColor(getPixel(source, x, y))
setColor(getPixel(target, targetX+x, targetY+y), px)
repaint(target)
return target

def rotatePic(pic):
width = getWidth(pic)
height = getHeight(pic)
canvas = makeEmptyPicture(height, width)
for x in range (0, width):
for y in range (0, height):
color = getColor(getPixel(pic, x, y))
setColor(getPixel(canvas, y, x), color)
return canvas

def mirrorQuad(pic):
width = getWidth(pic)
height = getHeight(pic)
canvas = makeEmptyPicture(width, height)
for x in range (0, width/2):
for y in range(0, height/2):
color = getColor(getPixel(pic, x, y))
setColor(getPixel(canvas, x, y), color)
setColor(getPixel(canvas, x, height-y-1), color)
setColor(getPixel(canvas, width-x-1, height-y-1), color)
setColor(getPixel(canvas, width-x-1, y), color)

def shrink(pic):
smallPic = makeEmptyPicture((getWidth(pic)/2), (getHeight(pic)/2))
for x in range (1, getWidth(pic)-1, 2):
for y in range (1, getHeight(pic)-1, 2):
px = getColor(getPixel(pic, x, y))
setColor(getPixel(smallPic, x/2, y/2), px)
return(smallPic)

def lightenUp(pic, userColor):


pixels = getPixels(pic)
for userColor in pixels:
oldColor = getColor(userColor)
newColor = makeLighter(oldColor)
setColor(userColor, newColor)
return pic

def roseColoredGlasses(pic):
pixels = getPixels(pic)

for p in pixels:
g = getGreen(p)
setGreen(p, g * 0.10)
b = getBlue(p)
setBlue(p, b * .80)

return(pic)

def betterBnW(pic):
pixels = getPixels(pic)
for p in pixels:
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
lum = r * 0.299 + b * 0.114 + g * 0.587
setRed (p, lum)
setGreen (p, lum)
setBlue (p, lum)
return pic
Red Eye Reduction (lab #6)

This function removes the red eye in a photo. To do this we check only for red
pixels within a range shown below. We then reduced the red for those pixels
to make the eyes appear normal color.
Function
def redEye(pic):
pixels = getPixels(pic)
for p in pixels:
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
if r > (b + g) and r > 128:
r = (b + g) / 2
setRed(p, r) # Reduce red

#Call Function
filename = pickAFile()
pic = makePicture(filename)
redEye(pic)
repaint(pic)
redEye
Art-i-fy (lab #6)
This function manipulates an image by reducing the colors used. This gives
the image a posterization effect. This is done by dividing each color, RGB, into
a set of given ranges.

Function
def ArtIFy(pic):
pixels = getPixels(pic)
for p in pixels:
# Fetch the existing pixel values and pass them
# to the transform before setting to the new val
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
if r < 64:
setRed(p, 31)
elif r < 128:
setRed(p, 95)
elif r < 192:
setRed(p, 159)
else:
setRed(p, 223)
if g < 64:
setGreen(p, 31)
elif g < 128:
setGreen(p, 95)
elif g < 192:
setGreen(p, 159)
else:
setGreen(p, 223)
if b < 64:
setBlue(p, 31)
elif b < 128:
setBlue (p, 95)
elif b < 192:
setBlue(p, 159)
else:
setBlue(p, 223)

#Call Function
filename = pickAFile()
pic = makePicture(filename)
ArtIFy(pic)
repaint(pic)
Green Screen (lab #6)

This function replaces the green screen with a background image. It involves
finding all the pixels that are the same color as the backdrop and replaces
them with a new image. The pixels don't necessarily have to be green, but this
has worked much more accurately than other colors.
Function
def chromaKey(pic, bgpic):
pixels = getPixels(pic)
bgPixels = getPixels(bgpic)
for p in pixels:
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
if g > (b + r):
setColor(p, getColor(getPixel(bgpic, getX(p), getY(p))))

#Problem 3,
filename = pickAFile()
pic = makePicture(filename)
background = pickAFile()
bgpic = makePicture(background)
chromaKey(pic, bgpic)
repaint(pic)

Thanksgiving Card (lab #7)


I had so much fun using the green screen (chroma key) function last time that
I decided to use it for the Thanksgiving card. I replaced the turkey's green
screen with a blue background and then added leaves on top, calling the
chroma key function again.
Function
#Removes green from image
def chromaKey(pic, bgpic):
pixels = getPixels(pic)
bgPixels = getPixels(bgpic)
for p in pixels:
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
if g > (b + r):
setColor(p, getColor(getPixel(bgpic, getX(p), getY(p))))

#calls chromaKey function


def makeCard():
filename = pickAFile()
pic = makePicture(filename)
background = pickAFile()
bgpic = makePicture(background)
chromaKey(pic, bgpic)
# repaint(pic)
writePictureTo(pic, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Lab
7\\ThanksgivingCard.jpg")
#Call makeCard() function
makeCard()

#Copying the image we created with the chromaKey function and using that as the background for
the final card
pic = makePicture("C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Lab
7\\ThanksgivingCard.jpg")
leavesPic = makePicture(pickAFile())
card = makeEmptyPicture(852, 480)

chromaKey(leavesPic, pic)

#pastes pic
for x in range(0, getWidth(pic)):
for y in range(0, getHeight(pic)):
color = getColor(getPixel(pic, x, y))
setColor(getPixel(card, x, y), color)
#pastes leavesPic
for x in range(0, getWidth(leavesPic)):
for y in range(0, getHeight(leavesPic)):
color = getColor(getPixel(leavesPic, x, y))
setColor(getPixel(card, x, y), color)

#adds text
fontStyle = makeStyle(serif, italic + bold, 70)
addTextWithStyle(card, 25, 450,'HAPPY THANKSGIVING!', fontStyle)

repaint (card)
writePictureTo(card, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Lab
7\\ThanksgivingCardFinal.jpg")
Line Drawing (Module 3)

The line drawing function first turns an image black and white. It then
compares the luminance of each pixel to the pixels below it and to the right of
it. If there is a large difference between the current pixel and the pixels to the
right and below, then the pixel is turned black. Otherwise it is turned white.

Function
def betterBnW(pic):
pixels = getPixels(pic)
for p in pixels:
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
l = r*0.299 + g*0.587 + b*0.114
setRed(p, l)
setGreen(p, l)
setBlue(p, l)

def lineDrawing():
filename = pickAFile()
pic = makePicture(filename)
width = getWidth(pic)
height = getHeight(pic)
drawing = makeEmptyPicture(width, height)
for x in range(0, width):
for y in range(0, height):
if(x == width-1):
rightPixel = getColor(getPixel(pic, x, y))
else:
rightPixel = getColor(getPixel(pic, x+1, y))
if(y == getHeight(pic)-1):
belowPixel = getColor(getPixel(pic, x, y))
else:
belowPixel = getColor(getPixel(pic, x, y+1))
p = getColor(getPixel(pic, x, y))

if(distance(p, rightPixel) <= 15 and distance(p, belowPixel) <= 15):


setColor(getPixel(drawing, x, y), white)
else:
setColor(getPixel(drawing, x, y), black)
repaint (drawing)

writePictureTo(drawing, "C:\\Users\\Grace\\Desktop\\CSUMB\\CST205\\Module 3\\Lab


7\\LineDrawing.jpg")

You might also like