Examples of swapping backgrounds using chromakey and editing regions of an image.
Download Complete Example
import media
def make_chromakey(pic, bg):
picture = media.duplicatePicture(pic)
for px in media.getPixels(picture):
x = media.getX(px)
y = media.getY(px)
if (media.getRed(px) + media.getGreen(px) < media.getBlue(px)):
bgpx = media.getPixel(bg, x, y)
media.setColor(px, media.getColor(bgpx))
return picture
def make_meme(pic, bg):
picture = media.duplicatePicture(pic)
for px in media.getPixels(picture):
x = media.getX(px)
y = media.getY(px)
if (media.getRed(px) + media.getBlue(px) < media.getGreen(px)):
bgpx = media.getPixel(bg, x, y)
media.setColor(px, media.getColor(bgpx))
return picture
def quarters(pic):
picture = media.duplicatePicture(pic)
width = media.getWidth(pic)
height = media.getHeight(pic)
for px in media.getPixels(picture):
x = media.getX(px)
y = media.getY(px)
if 0 <= x <= width//2 and 0 <= y <= height//2:
media.setRed(px, media.getRed(px) * .5)
elif width//2 <= x <= width - 1 and 0 <= y <= height//2:
media.setGreen(px, media.getGreen(px) * .5)
elif 0 <= x <= width//2 and height//2 <= y <= height - 1:
media.setBlue(px, media.getBlue(px) * .5)
else:
lum = media.getRed(px) + media.getGreen(px) + media.getBlue(px)
lum = lum // 3
media.setColor(px, media.makeColor(lum, lum, lum))
return picture
mark_chroma = media.makePicture('blue-mark.jpg')
fine_bg = media.makePicture('everything-is-fine.jpg')
media.show(mark_chroma)
media.show(make_chromakey(mark_chroma, fine_bg))
meme_dog = media.makePicture('advice_dog.png')
meme_bg = media.makePicture('meme_bg.jpg')
media.show(meme_dog)
media.show(make_meme(meme_dog, meme_bg))
flowers = media.makePicture('flowers.jpg')
media.show(flowers)
media.show(quarters(flowers))
media.quit()