From IPRE Wiki
Revision as of 03:25, 28 October 2009 by Ttthomas (Talk | contribs) ('''Changing Colors in your Image ''')

Jump to: navigation, search

Lab 08: Graphics Part II: Image Manipulation


  • Create functions to manipulate images

Image Transformations

You've probably used a variety of graphics editors such as Paint, Adobe Photoshop, GIMP, etc. to manipulate or enhance your pictures. In this lab session, you will be exploring and creating functions to transform your images in the same way these image editors are able to.

Image transformations are operations that change the appearance of an image or its spatial location. Common image transformations include resize (shrink or enlarge), rotate, crop, translate (i.e move), and flip. There are also functions to change the color or brightness of an image.

In this lab, we will explore three functions: resize, colorize and paste. At the end of this session, you will be able to use this functions to transform your images into beautiful mosaics. Once again, you are only limited by your imagination and creativity.

Resizing an Image: Shrinking and Enlarging

Below is a program that takes an image and shrink it by a specified factor, F. For instance, if the original image is 1000x1000 pixels and you wanted to shrink it by a factor of 5, you would end up with an image of size 200x200.

def main():
    # read an image and display it
    oldPic = makePicture(pickAFile())
    show(oldPic, "Before")
    X = getWidth(oldPic)
    Y = getHeight(oldPic)
    # Input the shrink factor and computer size of new image
    F = int(ask("Enter the shrink factor."))
    newx = X/F
    newy = Y/F
    # create the new image
    newPic = makePicture(newx, newy)
    for x in range(newx):
        for y in range(newy):
            setPixel(newPic, x, y, getPixel(oldPic, x*F, y*F))
    show(newPic, "After")

The image below on the right is 425x400 pixels in dimension. After shrinking it by a factor of 2, the result is the image on the right with dimensions 212x200.

Original Image (425x400) After Shrinking (212x200, F=2)

Exercise 1: Write a function (resize) that gives the user the option to enlarge or shrink an image by some factor. The user should have the option to choose this factor as well. Your function should display the new image after it has been resized.

Changing Colors in your Image

In the previous labs, you've manipulated the pixels in the pictures your robot take to identify various objects such as the orange pyramid. In this section you will experiment with changing colors in other images (these do not have to be pictures your robots take). The program below takes a butterfly image and changes two of the colors in the image. Recall that you can define your own colors using the makeColor function as shown below.

def changeColors():
    oldPic = makePicture(pickAFile())
    W = getWidth(oldPic)
    H = getHeight(oldPic)
    show(oldPic, "Original Pic")
    myRed = makeColor(254, 0, 0) #defining a new color

    for pixel in getPixels(oldPic):
        r, g, b = getRGB(pixel)
        if 200 < r <=255 and 0<=g <=100 and 0<=b <=100:
            #change red pixels to blue
            setColor(pixel, blue)
        elif 155 < r <=255 and 155 < g <=255 and 0<=b <=150:
            #change yellow pixels to red
            setColor(pixel, myRed)
    show(oldPic, "New Pic")

The images below show the transformation that was carried out by the code.

Butterfly1.JPG<--- Butterfly1 Butterfly2.JPG<--- Butterfly2

Exercise 2: Write a function called colorize that allows the user to randomly select 2 points in an image

Translating an Image

Copying and Pasting an Image

Assignment 08

Links to Course-Related Pages