APCO/IASC 1P00 Programs used in Lectures
Most are directly from the slides, as numbered
02-12-pick-and-play
def pickAndPlay():
myfile = pickAFile()
mysound = makeSound(myfile)
play(mysound)
02-14-pick-and-show
def pickAndShow():
myfile = pickAFile()
mypict = makePicture(myfile)
show(mypict)
05-3-decrease-red
def decreaseRed(pict):
for p in getPixels(pict):
# printNow("About to decrease red")
value=getRed(p)
setRed(p,value*0.5)
06-10-increase-red
def clearBlue(pict):
for p in getPixels(pict):
# value=getBlue(p) #Why don't we need this step??
setBlue(p,0)
06-12-sunset
def makeSunset(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p,value*0.7)
value=getGreen(p)
setGreen(p,value*0.7)
06-14-createNegative
def negative(picture):
for px in getPixels(picture):
red = getRed(px)
green = getGreen(px)
blue = getBlue(px)
negColour=makeColor( 255-red, 255-green, 255-blue)
setColor(px,negColour)
06-17-grayscale
def grayscale(picture):
for p in getPixels(picture):
intensity = (getRed(p) + getGreen(p) + getBlue(p)) / 3
setColor(p,makeColor(intensity,intensity,intensity))
06-19-grayscale
def grayscale(picture):
for p in getPixels(picture):
intensity = (getRed(p) + getRed(p) + getRed(p)) / 3
setColor(p,makeColor(intensity,intensity,intensity))
06-19-2-grayscale
def grayscale(picture):
for p in getPixels(picture):
intensity = (getRed(p) + getGreen(p) + getBlue(p)) / 3
setColor(p,makeColor(intensity,intensity,intensity))
06-19-grayscale-blue
def grayscaleBlue(picture):
for p in getPixels(picture):
intensity = (getBlue(p) + getBlue(p) + getBlue(p)) / 3
setColor(p,makeColor(intensity,intensity,intensity))
06-19-grayscale-red
def grayscaleRed(picture):
for p in getPixels(picture):
intensity = (getRed(p) + getRed(p) + getRed(p)) / 3
setColor(p,makeColor(intensity,intensity,intensity))
06-19-grayscale-with-update
picture=makePicture(pickAFile())
def grayScaleWithUpdate(picture):
show(picture)
count = 0
for p in getPixels(picture):
intensity = (getRed(p) + getRed(p) + getRed(p)) / 3
setColor(p,makeColor(intensity,intensity,intensity))
count = count + 1
# printNow(str(count))
if count % 1000 == 0: #Try 100 and 10000 as well
repaint(picture)
show(picture)
repaint(picture) # Needed as last pixels might
show(picture) # not be shown...
#Placing the command in line 1 results in it being
# executed when the LOAD button is pressed.
06-21-grayScaleNew
def grayScaleNew(picture):
for px in getPixels(picture):
newRed = getRed(px) * 0.299
newGreen = getGreen(px) * 0.587
newBlue = getBlue(px) * 0.114
luminance = newRed+newGreen+newBlue
setColor(px,makeColor(luminance,luminance,luminance))
def grayScaleNew(picture):
for px in getPixels(picture):
newRed = getRed(px) * 0.299
newGreen = getGreen(px) * 0.587
newBlue = getBlue(px) * 0.114
luminance = newRed+newGreen+newBlue
setColor(px,makeColor(luminance,luminance,luminance))
07-4-turnRed
def turnRed():
brown = makeColor(57, 16, 8)
pic = makePicture(pickAFile())
for px in getPixels(pic):
colour = getColor(px)
if distance(colour, brown) < 50.0:
redness = getRed(px)*1.5
setRed(px, redness)
show(pic)
return(pic)
07-18-sepiaTint
def sepiaTint(picture):
#Convert image to greyscale
grayScaleNew(picture)
#loop through picture to tint pixels
for p in getPixels(picture):
red = getRed(p)
blue = getBlue(p)
#tint shadows
if (red < 63):
red = red*1.1
blue = blue*0.9
#tint midtones
if (red > 62 and red < 192):
red = red*1.15
blue = blue*0.85
#tint highlights
if (red > 191):
red = red*1.08
if (red > 255):
red = 255
blue = blue*0.93
#set the new colour values
setBlue(p, blue)
setRed(p, red)
08-7-demo-nested-loops
def demoNestedLoops():
# Ask for the values that x and y will be given
for x in range(1, 3):
printNow("X is " + str(x))
for y in range(1, 4):
printNow(" and Y is " + str(y))
# Guess what the output will look like
# Change the range and re-run
08-15-turnRedInRange
def turnRedInRange():
brown = makeColor(57,16,8)
# file=r"C\Documents\mediasources\barbara.jpg"
picture=makePicture(pickAFile()) #Choose barbara.jpg
for x in range(70,168):
for y in range(56,190):
px=getPixel(picture,x,y)
colour = getColor(px)
if distance(colour,brown)<50.0:
redness=getRed(px)*1.5
setRed(px,redness)
show(picture)
return(picture)
09-6-makeSunset
def makeSunset(picture):
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(picture):
picture = makePicture(pickAFile())
for p in getPixels(picture):
value = getBlue(p)
setBlue(p, value*0.7)
def reduceGreen(picture):
for p in getPixels(picture):
value = getGreen(p)
setGreen(p, value*0.7)
09-8-makeSunset
def makeSunset(picture):
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(picture):
for p in getPixels(picture):
value = getBlue(p)
setBlue(p, value*0.7)
def reduceGreen(picture):
for p in getPixels(picture):
value = getGreen(p)
setGreen(p, value*0.7)
09-8-makeSunset2
def makeSunset2(picture):
reduceBlue(picture)
reduceGreen(picture)
def reduceBlue(fred):
for p in getPixels(fred):
value = getBlue(p)
setBlue(p, value*0.7)
def reduceGreen(wilma):
for p in getPixels(wilma):
value = getGreen(p)
setGreen(p, value*0.7)
09-12-marriage
def marry(husband, wife):
sayVows(husband)
sayVows(wife)
pronounce(husband, wife)
kiss(husband, wife)
def sayVows(speaker):
print "I, " + speaker + " blah blah"
def pronounce(man, woman):
print "I now pronounce you…"
def kiss(p1, p2):
if p1 == p2:
print "narcissism!"
# if p1 > p2:
# print p1 + " kisses " + p2
else:
print p1 + " kisses " + p2
10-3-loop-illustration
def loopIllustration():
for x in range(1,5):
print x,
def loopIllustration2():
for x in range(1,5):
print "------"
print "x = ", x
for y in range(20,26,2):
print y,
def loopIllustration3():
for x in range(1,5):
print "------"
print "X = ", x
for y in range(20,26,2):
print "Y = ", y
for z in range(40,50):
print z,
# > loopIllustration()
# then
# > LoopIllustration2()
# then
# > loopIllustration3()
10-3-red-eye (specific to a picture not generally available!)
def removeRedEye(pic, startX, startY, endX, endY, replacementColour):
red = makeColor(255, 0, 0)
for x in range(startX, endX):
for y in range(startY, endY):
currentPixel = getPixel(pic, x, y)
# if (distance(red, getColor(currentPixel)) < 165):
if (distance(red, getColor(currentPixel)) < 200):
setColor(currentPixel, replacementColour)
#
#Typical commands:
#e = makePicture(pickAFile())
#removeRedEye(e, 162, 142, 170, 148, makeColor(0,0,0))
# left eye, little square of black
#removeRedEye(e, 155, 135, 180, 155, makeColor(0,0,0))
# Not much difference to left eye!!
#Change to difference of 200
10-7-mirrorVertical
def mirrorVertical(source):
mirrorpoint = int(getWidth(source) / 2)
for y in range(1, getHeight(source)):
for xOffset in range(1, mirrorpoint):
pright = getPixel(source, mirrorpoint + xOffset, y)
pleft = getPixel(source, mirrorpoint - xOffset, y)
c = getColor(pleft)
setColor(pright, c)
10-9-mirrorHorizontal
def mirrorHorizontal(source):
mirrorpoint = int((getHeight(source)+1) / 2)
# printNow(mirrorpoint)
# count = 0
show(source)
for yOffset in range(1, mirrorpoint):
for x in range(1, getWidth(source)):
pbottom = getPixel(source, x, mirrorpoint + yOffset)
ptop = getPixel(source, x, mirrorpoint - yOffset)
setColor(pbottom, getColor(ptop))
# count = count + 1
# if (count % 1000) == 0:
# repaint(source)
# show(source)
return source
10-15-mirrorTemple
setMediaPath() #Choose folder containing “temple.jpg”
def mirrorTemple():
source = makePicture(getMediaPath("temple.jpg"))
# show(source)
# count = 0
mirrorpoint = 277
lengthToCopy = mirrorpoint - 14
for x in range(1, lengthToCopy):
for y in range(28, 98):
p1 = getPixel(source, mirrorpoint - x, y)
p2 = getPixel(source, mirrorpoint + x, y)
setColor(p2, getColor(p1))
# count = count + 1
# if (count % 100) == 0:
# repaint(source)
# show(source)
return source
#
#For Command Area:
# try mirrorTemple() and then show(source) Ooops!
# y=mirrorTemple() and then show(y) OK!!
11-03-posterize
pic = makePicture(pickAFile()) #edinburgh-castle.jpg
def posterize(picture):
#loop through the pixels
for p in getPixels(picture):
#get the RGB values
red = getRed(p)
green = getGreen(p)
blue = getBlue(p)
#check and set red values
if red < 64:
setRed(p, 31)
elif red < 128:
setRed(p, 95)
elif red < 192:
setRed(p, 159)
else:
setRed(p, 223)
#check and set green values
if green < 64:
setGreen(p, 31)
elif green < 128:
setGreen(p, 95)
elif green < 192:
setGreen(p, 159)
else:
setGreen(p, 223)
#check and set blue values
if blue < 64:
setBlue(p, 31)
elif blue < 128:
setBlue(p, 95)
elif blue < 192:
setBlue(p, 159)
else:
setBlue(p, 223)
11-08-pickFrom4Ranges
def pickFrom4Ranges(input, lowerbound, middlebound, upperbound, lowestvalue, lowvalue, highvalue, highestvalue):
#One assumes that the input varies from 0 to 255 inclusive, or else use
# if input < 0 or input > 255:
# return 'Error' equivalent...
if input < lowerbound:
return lowestvalue
elif input < middlebound:
return lowvalue
elif input < upperbound:
return highvalue
else:
return highestvalue
# print pickFrom4Ranges(21, 64, 128 ,192, 31, 95, 159, 223)
def posterize(picture):
#loop through the pixels
for p in getPixels(picture):
#get the RGB values
red = getRed(p)
green = getGreen(p)
blue = getBlue(p)
newvalue = pickFrom4Ranges(red,64,128,192,31,95,159,223)
setRed(p,newvalue)
newvalue = pickFrom4Ranges(green,64,128,192,31,95,159,223)
setGreen(p,newvalue)
newvalue = pickFrom4Ranges(blue,64,128,192,31,95,159,223)
setBlue(p,newvalue)
11-12-pickFromRanges
ranges= [[0,63,31],[64,127,95],[128,191,159], [192,255,223]]
def pickFromRanges(input,ranges):
for range in ranges:
printNow(range)
if input >= range[0] and input <= range[1]:
return range[2] #Remember return breaks out of the
# function
# If we get here, something weird happened, and 0 is
# treated as false
return 0
# Execute with, for example
# print pickFromRanges(200,ranges) # gives:
#[0, 63, 31]
#[64, 127, 95]
#[128, 191, 159]
#[192, 255, 223]
#223
11-15- grayScalePosterize
pic = makePicture(pickAFile())
def grayScalePosterize(pic):
for p in getPixels(pic):
r = getRed(p)
g = getGreen(p)
b = getBlue(p)
luminance = (r+g+b)/3
if luminance < 128:
setColor(p,black)
if luminance >= 128:
setColor(p,white)
11-18- lineDetect
file = pickAFile()
#Recipe 40, page 109
# Note the improved layout.
# In particular, note how pixels adjacent to the one of
# interest are addressed.
def lineDetect(filename):
original = makePicture(filename)
makeLD = makePicture(filename)
for x in range(1, getWidth(original)):
for y in range(1, getHeight(original)):
here = getPixel(makeLD, x, y)
below = getPixel(original, x, y+1)
right = getPixel(original, x+1, y)
hereI = (getRed(here) + getGreen(here) + getBlue(here))/3
belowI = (getRed(below) + getGreen(below) + getBlue(below))/3
rightI = (getRed(right) + getGreen(right) + getBlue(right))/3
if (abs(hereI - belowI) > 2) and (abs(hereI - rightI) > 2):
setColor(here, black)
else:
setColor(here, white)
show(makeLD)
return makeLD
#Note: This only works at the edges because we don't access
# the last pixel in x or in y direction (i.e. range is
# one less than the width or height)
12-19-copy-barb-to-canvas
def copyBarb():
# Set up the source and target pictures
barbf=getMediaPath("barbara.jpg") #The file first
barb = makePicture(barbf)
canvasf = getMediaPath("7inX95in.jpg")
canvas = makePicture(canvasf)
# Now, do the actual copying
targetX = 100 #instead of 1
for sourceX in range(1,getWidth(barb)):
targetY = 100 #instead of 1
for sourceY in range(1,getHeight(barb)):
colour = getColor(getPixel(barb,sourceX,sourceY))
setColor(getPixel(canvas,targetX,targetY), colour)
targetY = targetY + 1
targetX = targetX + 1
show(barb)
show(canvas)
return canvas
#
# setMediaPath() first
12-34-chromakey
src = makePicture(pickAFile()) # and choose
# weather-green-background.jpg
bg = makePicture(pickAFile()) # and choose weather-map.jpg
def chromakey(source,bg):
# source should have something in front of green, bg is the
# new background
bgcolour = makeColor(25, 253, 27)
for x in range(1,getWidth(source)):
for y in range(1,getHeight(source)):
p = getPixel(source,x,y)
# My defn. of green: If the redness + blueness < greenness (in book)
# if ((getRed(p) + getBlue(p)) < getGreen(p)): # leads to
# jagged edges
if (getColor(p) == bgcolour): # But this was not the
# colour close to the man! SO
# if distance(getColor(p), bgcolour) <= 100:
#Then, grab the colour at the same spot from the new b/g
setColor(p,getColor(getPixel(bg,x,y)))
# e.g.:
#> print distance (makeColor(78,221,69), makeColor(25,253,27))
# 74.81310045707235
1 of 14