transform a picture
# This is a test program that saves us from having to keep typing pickAFile() etc.
# in the command area
def test():
fileName = pickAFile()
print fileName
picture = makePicture(fileName)
print picture
show(picture)
transform(picture)
repaint(picture)
# This is a function that transforms a picture by changing all the pixels in it in
# exactly the same way.
def transform(pic):
aBigListOfPixels = getPixels(pic)
for pixel in aBigListOfPixels:
pass # Replace this line with the function that transforms the pixel in the right way
# for example darken(pixel)
def darken(pixel): # Darken a pixel by reducing its RGB values
setRed(pixel, getRed(pixel)/2)
setGreen(pixel, getGreen(pixel)/2)
setBlue(pixel, getBlue(pixel)/2)
def negative(pixel): # Make a pixel its complementary color
setRed(pixel, 255-getRed(pixel))
setGreen(pixel, 255-getGreen(pixel))
setBlue(pixel, 255-getBlue(pixel))
def bw(pixel): #Make a pixel gray
value = (getRed(pixel)+getGreen(pixel)+getBlue(pixel))/3.0
grayTone = makeColor(value, value, value)
setColor(pixel, grayTone)
def bw2(pixel): #Make a pixel gray, simulating an orange contrast filter
value = (getRed(pixel)*0.5+getGreen(pixel)+getBlue(pixel)*1.5)/3.0
grayTone = makeColor(value, value, value)
setColor(pixel, grayTone)
def posterize(pixel): # Change color of pixel to one from a smaller palette
# This posterizing function (unlike the homework) changes all colors into primary
# colors, secondary colors, black, or white.
if getRed(pixel) > 127:
setRed(pixel, 255)
else:
setRed(pixel, 0)
if getGreen(pixel) > 127:
setGreen(pixel, 255)
else:
setGreen(pixel, 0)
if getBlue(pixel) > 127:
setBlue(pixel, 255)
else:
setBlue(pixel, 0)
Link to this Page