Examples of using media.distance and conditionals.
Download Complete Example
import media
def paint_red_door():
filename = 'redDoor.jpg'
picture = media.makePicture(filename)
media.explore(picture)
for pixel in media.getPixels(picture):
if media.distance(media.getColor(pixel), media.red) < 150:
media.setRed(pixel, media.getRed(pixel) * .2)
media.setGreen(pixel, media.getGreen(pixel) * 2)
media.setBlue(pixel, media.getBlue(pixel) * 2)
media.explore(picture)
def paint_red_door_region():
filename = 'redDoor.jpg'
picture = media.makePicture(filename)
media.explore(picture)
for pixel in media.getPixels(picture):
x = media.getX(pixel)
y = media.getY(pixel)
if 153 <= x <= 284 and 80 <= y <= 375:
if media.distance(media.getColor(pixel), media.red) < 200:
media.setRed(pixel, media.getRed(pixel) * .2)
media.setGreen(pixel, media.getGreen(pixel) * 2)
media.setBlue(pixel, media.getBlue(pixel) * 2)
media.explore(picture)
def remove_red_eye():
filename = 'red-eye.jpg'
picture = media.makePicture(filename)
media.explore(picture)
for pixel in media.getPixels(picture):
x = media.getX(pixel)
y = media.getY(pixel)
if (160 <= x <= 196 and 197 <= y <= 232) or (459 <= x <= 496 and 196 <= y <= 234):
if media.distance(media.getColor(pixel), media.red) < 200:
media.setRed(pixel, 0)
media.setGreen(pixel, 0)
media.setBlue(pixel, 0)
media.explore(picture)
def convert_gray_scale(picture):
for pixel in media.getPixels(picture):
red = media.getRed(pixel) * 0.299
green = media.getGreen(pixel) * 0.587
blue = media.getBlue(pixel) * 0.114
luminance = red + green + blue
media.setColor(pixel, media.makeColor(luminance, luminance, luminance))
def convert_sepia():
filename = 'country-side.jpg'
picture = media.makePicture(filename)
media.show(picture)
convert_gray_scale(picture)
media.show(picture)
for pixel in media.getPixels(picture):
red = media.getRed(pixel)
blue = media.getBlue(pixel)
if red < 63:
red = red * 1.1
blue = blue * 0.9
elif 62 < red < 192:
red = red * 1.25
blue = blue * 0.85
else:
red = red * 1.08
blue = blue * 0.93
media.setBlue(pixel, blue)
media.setRed(pixel, red)
media.show(picture)
def posterize():
filename = 'mccaw.jpg'
picture = media.makePicture(filename)
media.show(picture)
for pixel in media.getPixels(picture):
red = media.getRed(pixel)
green = media.getGreen(pixel)
blue = media.getBlue(pixel)
if(red < 64):
media.setRed(pixel, 31)
if(red > 63 and red < 128):
media.setRed(pixel, 95)
if(red > 127 and red < 192):
media.setRed(pixel, 159)
if(red > 191 and red < 256):
media.setRed(pixel, 223)
if(green < 64):
media.setGreen(pixel, 31)
if(green > 63 and green < 128):
media.setGreen(pixel, 95)
if(green > 127 and green < 192):
media.setGreen(pixel, 159)
if(green > 191 and green < 256):
media.setGreen(pixel, 223)
if(blue < 64):
media.setBlue(pixel, 31)
if(blue > 63 and blue < 128):
media.setBlue(pixel, 95)
if(blue > 127 and blue < 192):
media.setBlue(pixel, 159)
if(blue > 191 and blue < 256):
media.setBlue(pixel, 223)
media.show(picture)
paint_red_door()
paint_red_door_region()
remove_red_eye()
convert_sepia()
posterize()
media.quit()