Examples of copying and transforming an image.
Download Complete Example
import media
def scale_up_region(original_image, start_x, start_y, end_x, end_y, factor):
width = media.getWidth(original_image)
height = media.getHeight(original_image)
new_canvas = media.makeEmptyPicture((end_x - start_x) * factor, (end_y - start_y) * factor)
dest_x = 0
for source_x in range(start_x, end_x):
dest_y = 0
for source_y in range(start_y, end_y):
source_pixel = media.getPixel(original_image, source_x, source_y)
source_color = media.getColor(source_pixel)
for sub_x in range(factor):
for sub_y in range(factor):
dest_pixel = media.getPixel(new_canvas, dest_x + sub_x, dest_y + sub_y)
media.setColor(dest_pixel, source_color)
dest_y += factor
dest_x += factor
return new_canvas
def blur(original_image):
target = media.duplicatePicture(original_image)
for x in range(1, media.getWidth(original_image)-1):
for y in range(1, media.getHeight(original_image)-1):
top = media.getPixel(original_image, x, y-1)
left = media.getPixel(original_image, x-1, y)
right = media.getPixel(original_image, x+1, y)
bottom = media.getPixel(original_image, x, y+1)
center = media.getPixel(target, x, y)
newRed= (media.getRed(top) + media.getRed(left) + media.getRed(bottom) +
media.getRed(right) + media.getRed(center)) // 5
newGreen= (media.getGreen(top) + media.getGreen(left) + media.getGreen(bottom) +
media.getGreen(right) + media.getGreen(center)) // 5
newBlue= (media.getBlue(top) + media.getBlue(left) + media.getBlue(bottom) +
media.getBlue(right) + media.getBlue(center)) // 5
media.setColor(center, media.makeColor(newRed, newGreen, newBlue))
return target
def basic_blend(pic1, pic2):
new_canvas = media.makeEmptyPicture(min(media.getWidth(pic1),media.getWidth(pic2)),
min(media.getHeight(pic1),media.getHeight(pic2)))
canvas_width = media.getWidth(new_canvas)
canvas_height = media.getHeight(new_canvas)
for x in range(canvas_width):
for y in range(canvas_height):
canvas_pixel = media.getPixel(new_canvas, x, y)
pic1_pixel = media.getPixel(pic1, x, y)
pic2_pixel = media.getPixel(pic2, x, y)
new_red = 0.70*media.getRed(pic1_pixel) + 0.30*media.getRed(pic2_pixel)
new_green = 0.70*media.getGreen(pic1_pixel) + 0.30*media.getGreen(pic2_pixel)
new_blue = 0.70*media.getBlue(pic1_pixel) + 0.30*media.getBlue(pic2_pixel)
media.setColor(canvas_pixel, media.makeColor(new_red, new_green, new_blue))
return new_canvas
def partial_blend(pic1, pic2):
pic1_width = media.getWidth(pic1)
pic1_height = media.getHeight(pic1)
pic2_width = media.getWidth(pic2)
pic2_height = media.getHeight(pic2)
unblended_amount = 300
overlap = pic1_width - unblended_amount
new_canvas = media.makeEmptyPicture(pic1_width + pic2_width - overlap,
max(pic1_height, pic2_height))
source_x = 0
for target_x in range(0, unblended_amount):
source_y = 0
for target_y in range(0, media.getHeight(pic1)):
color = media.getColor(media.getPixel(pic1, source_x, source_y))
media.setColor(media.getPixel(new_canvas, target_x, target_y), color)
source_y += 1
source_x += 1
source_x = 0
for target_x in range(unblended_amount, media.getWidth(pic1)):
source_y = 0
pic1_pixel = media.getPixel(pic1, source_x + unblended_amount, source_y)
new_red = 0.50*media.getRed(pic1_pixel) + 0.50*media.getRed(pic2_pixel)
new_green = 0.50*media.getGreen(pic1_pixel) + 0.50*media.getGreen(pic2_pixel)
new_blue = 0.50*media.getBlue(pic1_pixel) + 0.50*media.getBlue(pic2_pixel)
color = media.makeColor(new_red, new_green, new_blue)
media.setColor(media.getPixel(new_canvas, target_x, target_y), color)
source_y = source_y + 1
source_x = source_x + 1
source_x = overlap
for target_x in range(unblended_amount+overlap, unblended_amount+media.getWidth(pic2)):
source_y = 0
for target_y in range(0,media.getHeight(pic2)):
color = media.getColor(media.getPixel(pic2, source_x, source_y))
media.setColor(media.getPixel(new_canvas, target_x, target_y), color)
source_y = source_y + 1
source_x = source_x + 1
return new_canvas
def drawing():
new_canvas = media.makeEmptyPicture(800, 600)
media.addLine(new_canvas, 0, 0, 100, 100)
media.addLine(new_canvas, 0, 100, 200, 50, media.green)
media.addText(new_canvas, 300, 10, 'Drawing Examples')
media.addText(new_canvas, 300, 30, 'FUN TIME!', media.magenta)
font_style = media.makeStyle(media.mono, media.italic, 24)
media.addTextWithStyle(new_canvas, 400, 550, "Fancy Text!", font_style, media.blue)
media.addRect(new_canvas, 300, 50, 60, 90)
media.addRect(new_canvas, 375, 50, 60, 90, media.magenta)
media.addRectFilled(new_canvas, 300, 160, 50, 50)
media.addRectFilled(new_canvas, 375, 160, 100, 100, media.yellow)
media.addOval(new_canvas, 575, 150, 90, 90)
media.addOval(new_canvas, 675, 200, 100, 100, media.orange)
media.addOvalFilled(new_canvas, 600, 50, 70, 70)
media.addOvalFilled(new_canvas, 700, 10, 70, 150, media.cyan)
media.addArc(new_canvas,500, 350, 120, 120, 0, 90)
media.addArc(new_canvas, 150, 350, 100, 100, 180, 90, media.blue)
media.addArcFilled(new_canvas, 400, 350, 100, 100, 0, 180)
media.addArcFilled(new_canvas, 250, 350, 100, 100, 45, 90, media.green)
return new_canvas
lizard = media.makePicture('images/lizard.jpg')
ostrich = media.makePicture('images/ostrich.jpg')
media.show(lizard)
face = scale_up_region(lizard, 252, 82, 364, 169, 3)
print("Write blurred lizard to file")
media.writePictureTo(blur(face), 'out.jpg')
media.show(face)
print("Write blurred ostrich to file")
media.writePictureTo(blur(ostrich), 'out.jpg')
media.show(ostrich)
wall = media.makePicture('images/wood_wall.jpg')
rainbow = media.makePicture('images/rainbow.jpg')
media.show(wall)
media.show(rainbow)
media.show(basic_blend(wall, rainbow))
parrot = media.makePicture('images/parrot.jpg')
forest = media.makePicture('images/forest.jpg')
media.show(forest)
media.show(parrot)
media.show(partial_blend(forest, parrot))
media.show(drawing())
media.quit()