Code from Bill Leahy's 9/10 lecture
# Example demonstrating use of setMediaPath and getMediaPath
def grayscale(picture):
for px in getPixels(picture):
newRed = getRed(px) * 0.299
newGreen = getGreen(px) * 0.587
newBlue = getBlue(px) * 0.114
lum = newRed + newGreen + newBlue
setColor(px,makeColor(lum, lum, lum))
def posterize(pic):
#loop through the pixels
for p in getPixels(pic):
#get the RGB values
r = posterizeHelper(getRed(p))
g = posterizeHelper( getGreen(p))
b = posterizeHelper(getBlue(p))
setRed(p, r)
setGreen(p, g)
setBlue(p, b)
def posterizeHelper(c):
if(c < 64):
c = 31
if(c > 63 and c < 128):
c = 95
if(c > 127 and c < 192):
c = 159
if(c > 191 and c < 256):
c = 223
return c
def hiContrast():
file = getMediaPath('redeye.jpg')
pic = makePicture(file)
grayscale(pic)
posterize(pic)
show(pic)
return(pic)
# Anyone wanting to run this code would first type:
#
# setMediaPath()
#
# A chooser window would open allowing choice of a directory (folder)
# Not a file name!
# The path to that directory would be stored in the innards of JES
# as a string of charatcers
# The getMediaPath function would retrieve that string and add to
# it the filename in the function call.
# This function goes through a picture and changes the color of every
# pixel depending on how far it is from the target. Just used to get
# an idea if simple color replacement recipes will work very well.
def contour(pic):
target = red
for px in getPixels(pic):
if distance(target, getColor(px)) >= 300:
c = black
if distance(target, getColor(px)) < 300:
c = red
if distance(target, getColor(px)) < 250:
c = orange
if distance(target, getColor(px)) < 200:
c = yellow
if distance(target, getColor(px)) < 150:
c = green
if distance(target, getColor(px)) < 100:
c = blue
if distance(target, getColor(px)) < 50:
c = magenta
setColor(px, c)
# Better than Visine for getting the red out!
def redeye(pic, startX, startY, stopX, stopY, newcolor):
target = red
for x in range(startX, stopX):
for y in range(startY, stopY):
px = getPixel(pic, x, y)
if distance(target, getColor(px)) < 200:
setColor(px, newcolor)
def verticalMirror(pic):
mirrorpoint = int(getWidth(pic)/2)
for y in range(1, getHeight(pic)):
for x in range(1, mirrorpoint):
xleft = mirrorpoint - x
xright = mirrorpoint + x
pleft = getPixel(pic, xleft, y)
pright = getPixel(pic, xright, y)
c = getColor(pleft)
setColor(pright, c)
def horizontalMirror(pic):
mirrorpoint = int(getHeight(pic)/2)
for x in range(1, getWidth(pic)):
for y in range(1, mirrorpoint):
ytop = mirrorpoint - y
ybottom = mirrorpoint + y
ptop = getPixel(pic, x, ytop)
pbottom = getPixel(pic, x, ybottom)
c = getColor(ptop)
setColor(pbottom, c)
Link to this Page