athletics67
11/11/2018 - 7:25 PM

makeCollage.py

import random

def makeCollage():
  i = 0
  targetX = 0
  targetY = 0
  canvas = makeEmptyPicture(2550, 3300)
  func_list = [mirrorVrt, mirrorQuad, mirrorHrz1, mirrorHrz2, betterBnW, 
               makeNegative, lightenUp, roseColoredGlasses] 
  while i <= 7:
    source = getPic()
    targetX = int(requestNumber("Enter X Coordinate: "))
    targetY = int(requestNumber("Enter Y Coordinate: "))
    x = func_list.pop(random.randrange(len(func_list)))
    x(source)
    pyCopy(source, canvas, targetX, targetY)
    i += 1
  repaint(canvas)
  
# Image manipulation functions from previous labs

# Vertical mirror of image
def mirrorVrt(pic):
  for x in range(0, getWidth(pic)/2):
    for y in range(0, getHeight(pic)):
      color = getColor(getPixel(pic, x, y))
      setColor(getPixel(pic, getWidth(pic)-x-1, y),color)
  return

# Quadruple mirror of image
def mirrorQuad(pic):
  for x in range(0, getWidth(pic)/2):
    for y in range(0, getHeight(pic)/2):
      color = getColor(getPixel(pic, x, y))
      setColor(getPixel(pic, getWidth(pic)-x-1, y),color)
      setColor(getPixel(pic, x, getHeight(pic)-y-1),color)
      setColor(getPixel(pic, getWidth(pic)-x-1, getHeight(pic)-y-1),color)
  return

# Horizontal mirror of image (top to bottom)
def mirrorHrz1(pic):
  for x in range(0, getWidth(pic)):
    for y in range(0, getHeight(pic)/2):
      color = getColor(getPixel(pic, x, y))
      setColor(getPixel(pic, x, getHeight(pic)-y-1),color)
  return
  
def mirrorHrz2(pic):
  for x in range(0, getWidth(pic)):
    for y in range(getHeight(pic)/2, getHeight(pic)):
      color = getColor(getPixel(pic, x, y))
      setColor(getPixel(pic, x, getHeight(pic)-y-1),color)
  return

# Black/White
def betterBnW(pic):
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
    luminance = r*0.299 + g*0.587 + b*0.114
    setRed(p,luminance)
    setGreen(p,luminance)
    setBlue(p,luminance)
  return

# Negative image
def makeNegative(pic):
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
    setRed(p,255 - r)
    setGreen(p,255 - g)
    setBlue(p,255 - b)
  return

# Lighten image
def lightenUp(pic):
  pixels = getPixels(pic)
  for p in pixels:
    oldColor = getColor(p)
    newColor = makeLighter(oldColor)
    setColor(p,newColor)
  return

# Rose colored image
def roseColoredGlasses(pic):
  pixels = getPixels(pic)
  for p in pixels:
    r = getRed(p)
    g = getGreen(p)
    b = getBlue(p)
    setRed(p,g * 1.25)
    setGreen(p,g * 0.5)
    setBlue(p,b * 0.5)
  return