A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image...

20
A Media Computation A Media Computation Cookbook Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing colors in an area Part 3: Chromakey for digital video effects Part 4: Manipulated Alice images Part 5: Sound manipulations Part 6: Manipulated sounds in Alice

Transcript of A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image...

Page 1: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

A Media Computation A Media Computation CookbookCookbookManipulating Images and Sounds for Use in AlicePart 1: Image ManipulationsPart 2: Advanced Image Manipulations, e.g., changing colors in an areaPart 3: Chromakey for digital video effectsPart 4: Manipulated Alice imagesPart 5: Sound manipulationsPart 6: Manipulated sounds in Alice

Page 2: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Questions being answered Questions being answered here todayhere todayHow do we make it easier to build new

things?How do we make it easy to write a

program to access specific pictures?How do we change the size of a picture?How do grab part of a picture?How do you combine parts of pictures?How do we use programming to replace

re-typing code?How do we replace the background of a

picture with something else?

Page 3: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

How do we make it easier to How do we make it easier to build new things?build new things?Don’t start from scratch!Use the old file and add more to it.

When you start JES, open up the file that you used last time.◦ If you end your file name with “.py” you can figure

out which are Python files.Type more code into that file.

If you want, you can download my myfunctions.py from http://coweb.cc.gatech.edu/mediaComp-teach/43

Page 4: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

How do we make it easy to How do we make it easy to write a program to access write a program to access certain pictures?certain pictures?setMediaPath()

◦ Type this in the Command Area (black area at bottom of JES) and find the folder where you store your pictures.

◦ (Could be the Desktop, probably better to use a particular folder. I call mine “mediasources”.)

makePicture(getMediaPath(“IMG_0810.JPG”))◦ Will make a picture from that file in the

“media path” folder.

Page 5: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

How do we change the size How do we change the size of a picture?of a picture?To copy a picture, get the color

values from pixels in one picture, and set those color values in the pixels in the other picture.

def copyPicture(picture):

returnPicture = makeEmptyPicture(getWidth(picture),getHeight(picture))

for pixel in getPixels(picture):

color = getColor(pixel)

setColor(getPixel(returnPicture,getX(pixel),getY(pixel)),returnPicture)

return returnPicture

Page 6: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

To scale the pictureTo scale the picturesmaller = scale(picture,0.5)How does it work?

◦To get it to be only ½ the size (in both horizontal and vertical) directions, we need to lose some pixels.

◦Easiest way: Skip a few. Every other pixel gets copied.

◦Can generalize this for any size scaling.

Page 7: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Code to scaleCode to scaledef scale(picture,factor):

newHeight = int(factor*getHeight(picture))+1

newWidth = int(factor*getWidth(picture))+1

returnPic = makeEmptyPicture(int(newWidth),int(newHeight))

sx = 0

for tx in range(0,newWidth):

sy = 0

for ty in range(0,newHeight):

if (int(sx) < getWidth(picture)) and (int(sy) < getHeight(picture)):

sp = getPixel(picture,int(sx),int(sy))

tp = getPixel(returnPic,tx,ty)

setColor(tp,getColor(sp))

sy = sy + (1/factor)

sx = sx + (1/factor)

return returnPic

Page 8: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

How do we grab How do we grab partpart of a of a picture?picture? guys =

makePicture(getMediaPath("IMG_0805.JPG"))james =

copyPartPicture(guys,352,217,618,475)

copyPartPicture(picture,startX,startY,endX,endY)◦ Gives you a new picture inside the box defined by

upper-left corner and lower-right corner.

Page 9: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Code to grab part of a Code to grab part of a picturepicturedef copyPartPicture(picture,x1,y1,x2,y2):

returnPicture = makeEmptyPicture(x2-x1,y2-y1)

targetx = 0

for srcx in range(x1,x2):

targety = 0

for srcy in range(y1,y2):

srcpixel = getPixel(picture,srcx,srcy)

targetPixel = getPixel(returnPicture,targetx,targety)

setColor(targetPixel,getColor(srcpixel))

targety = targety + 1

targetx = targetx + 1

return returnPicture

Page 10: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

How do we combine parts of How do we combine parts of a picture?a picture?grayscale(betsy)clearRed(james)pastePicture(betsy,scale(james,0.3

5),0,0)

pastePicture(canvas,partial,startX,startY)◦Pasting “partial” picture onto the

“canvas” starting from (startX,startY)

Page 11: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Code to paste a pictureCode to paste a picturedef pastePicture(canvas,source,startx,starty):

targetx = startx

for x in range(0,getWidth(source)):

targety = starty

for y in range(0,getHeight(source)):

srcpixel = getPixel(source,x,y)

if (targetx < getWidth(canvas)) and (targety < getHeight(canvas)):

targetPixel = getPixel(canvas,targetx,targety)

setColor(targetPixel,getColor(srcpixel))

targety = targety + 1

targetx = targetx + 1

Page 12: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

How do use programming to How do use programming to replace re-typing code?replace re-typing code?Building a collage of pictures:def makeACollage():

betsy = makePicture(getMediaPath("IMG_0802.JPG"))

hunter = makePicture(getMediaPath("IMG_0808.JPG"))

guys = makePicture(getMediaPath("IMG_0805.JPG"))

james = copyPartPicture(guys,352,217,618,475)

grayscale(betsy)

clearRed(james)

pastePicture(betsy,scale(james,0.35),0,0)

maxBlue(guys)

pastePicture(betsy,scale(guys,0.35),450,340)

negative(hunter)

pastePicture(betsy,scale(hunter,0.35),0,340)

writePictureTo(betsy,"C:/collage.jpg")

Page 13: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Result:Result:

Page 14: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

How do we replace the How do we replace the background of a picture with background of a picture with something else?something else?Take a picture with a background that

is easy to test and isn’t in the foreground.

Two examples. Which one do you think will work?

Page 15: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Code to chromakey for any Code to chromakey for any background colorbackground colordef chromakey(picture,background,color,threshold=100.0):

for pixel in getPixels(picture):

pixelColor = getColor(pixel)

if distance(color,pixelColor) < threshold:

newColor = getColor(getPixel(background,getX(pixel),getY(pixel)))

setColor(pixel,newColor)

Page 16: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Trying to make the yellow Trying to make the yellow background workbackground work>>> chromakey(yellowBarb,

moon, makeColor(167,159,110))

>>> writePictureTo(yellowBarb, "C:/yellowBarb-th100.jpg")

>>> chromakey( yellowBarb, moon, makeColor(167,159,110),50)

>>> writePictureTo( yellowBarb, "C:/yellowBarb-th50.jpg")

Yellow is too close to Barb’s skin tone to work.

Page 17: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Using the general chromakey Using the general chromakey form for a blue backgroundform for a blue background>>>

chromakey(blueJenny,moon,makeColor(36,62,95))

Page 18: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Doing a specific version of Doing a specific version of ChromakeyChromakeyComing up with a test for being

“truly blue”

def chromakeyBlue(picture,background):

for pixel in getPixels(picture):

pixelColor = getColor(pixel)

if getRed(pixel)<getBlue(pixel) and getGreen(pixel)<getBlue(pixel):

newColor = getColor(getPixel(background,getX(pixel),getY(pixel)))

setColor(pixel,newColor)

Page 19: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Nicely chromakeyedNicely chromakeyed

Page 20: A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.

Try it!Try it!Take your own picture and put it

on a new background.Create a collage with various

pieces of the pictures we have.