Examples of copying and transforming an image.
Download Complete Example
import media
def copy_image(original_image, start_x, start_y):
new_canvas = media.makeEmptyPicture(800,500)
dest_x = start_x
for source_x in range(media.getWidth(original_image)):
dest_y = start_y
for source_y in range(media.getHeight(original_image)):
source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)
dest_y = dest_y + 1
dest_x = dest_x + 1
return new_canvas
def crop_image(original_image, tl_x, tl_y, br_x, br_y):
new_canvas = media.makeEmptyPicture(br_x - tl_x, br_y - tl_y)
dest_x = 0
for source_x in range(tl_x, br_x):
dest_y = 0
for source_y in range(tl_y, br_y):
source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)
dest_y = dest_y + 1
dest_x = dest_x + 1
return new_canvas
def flip_vertical(original_image):
# Get the width and height of the image
width = media.getWidth(original_image)
height = media.getHeight(original_image)
new_canvas = media.makeEmptyPicture(width, height)
dest_x = width - 1
for source_x in range(width):
dest_y = 0
for source_y in range(height):
source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)
dest_y = dest_y + 1
dest_x = dest_x - 1
return new_canvas
def flip_horizontal(original_image):
width = media.getWidth(original_image)
height = media.getHeight(original_image)
new_canvas = media.makeEmptyPicture(width, height)
dest_x = 0
for source_x in range(width):
dest_y = height - 1
for source_y in range(height):
source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)
dest_y = dest_y - 1
dest_x = dest_x + 1
return new_canvas
def rotate_clockwise(original_image):
width = media.getWidth(original_image)
height = media.getHeight(original_image)
new_canvas = media.makeEmptyPicture(height, width)
dest_y = 0
for source_x in range(width):
dest_x = height - 1
for source_y in range(height):
source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)
dest_x = dest_x - 1
dest_y = dest_y + 1
return new_canvas
def scale_down(original_image):
# Get the width and height of the image
width = media.getWidth(original_image)
height = media.getHeight(original_image)
new_canvas = media.makeEmptyPicture(width//2, height//2)
source_x = 0
for dest_x in range(0, width//2):
source_y = 0
for dest_y in range(0, height//2):
source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)
source_y = source_y + 2
source_x = source_x + 2
return new_canvas
def scale_up(original_image):
width = media.getWidth(original_image)
height = media.getHeight(original_image)
new_canvas = media.makeEmptyPicture(width*2, height*2)
source_x = 0
for dest_x in range(0, width*2):
source_y = 0
for dest_y in range(0, height*2):
source_color = media.getColor(media.getPixel(original_image, int(source_x), int(source_y)))
media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)
source_y = source_y + 0.5
source_x = source_x + 0.5
return new_canvas
def scale_up_general(original_image, factor):
# Get the width and height of the image
width = media.getWidth(original_image)
height = media.getHeight(original_image)
new_canvas = media.makeEmptyPicture(width*factor, height*factor)
dest_x = 0
for source_x in range(width):
dest_y = 0
for source_y in range(height):
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 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
lizard = media.makePicture('images/lizard.jpg')
ostrich = media.makePicture('images/ostrich.jpg')
media.show(lizard)
media.show(copy_image(lizard, 50, 50))
media.show(lizard)
media.show(crop_image(lizard, 252, 82, 364, 169))
# rotations
media.show(ostrich)
media.show(flip_vertical(ostrich))
media.show(ostrich)
media.show(flip_horizontal(ostrich))
media.show(ostrich)
media.show(rotate_clockwise(ostrich))
media.show(lizard)
media.show(scale_down(lizard))
media.show(lizard)
media.show(scale_up(lizard))
media.show(lizard)
media.writePictureTo(scale_up_general(lizard, 3), 'output.jpg')
media.show(lizard)
media.show(scale_up_region(lizard, 252, 82, 364, 169, 2))
media.quit()