A Picture is a Matrix

These notes describe how to write Python programs that perform photoshop-like operations on images. Before tackling these notes, make sure you have an understanding of iteration, matrices, and the basics of the JES Media programming environment.

Representing Images in Digital Form  or 'A Picture is worth a Thousand Pixels!'

                            col=1            2                    3            4                5

 row=1 RGB RGB RGB RGB RGB
 row=2 RGB RGB RGB RGB RGB
 row=3 RGB RGB RGB RGB RGB
 row=4 RGB RGB RGB RGB RGB

In the JES Media system, an image consists of a table (matrix) of pixels. The left top corner of the image is coordinate col=1,row=1 (not 0)                     
A pixel is a color. Colors are defined with three attributes: the amount of red, green, blue. We call this an RGB value.

 
Each of the RGB attributes are between 0 and 255
 
    {0,0,0} is black.
 
    {255,255,255} is white
 
    What is Red? Blue? Green? Purple?


Check out the JES Tool for viewing the pixels of a picture:

1.With JES, Load an image using the makePicture function: 
 
filename=pickAFile()
pic=makePicture(filename))
 
2. Choose Media Tools | Picture Tool. When the picture appears, click on it. The environment will display the x-y coordinate and RGB values for each pixel. This can help you find the RGB value of colors you want to use.

Key Manipulation Functions


    makeColor(r,g,b)  -> this returns a color, e.g.,
 
         white=makeColor(255,255,255)
 
    getPixel(pic,x,y)   -> this returns a pixel from an image, e.g.,
 
         pixel=getPixel(pic,5,7)

    setColor
(pixel, color)


           setColor(pixel,white)


Manipulating Images Pixel-by-Pixel

Suppose you wanted to draw a white square, of size 100x100, in the top left corner of an image:

We must do something-- paint a pixel-- many times. So we'll iterate:


    i=0
    while i<100:
        # do something
        i=i+1


In this case, we want to  begin with the first column, and iterate over the first 100 pixels in it (the first 100 rows). Then we move to the second column, and color its first 100 rows white. We then get the next column, and work on its first 100 rows. You might think of it as:

    col=1
    row =1
    while row<100:
        # color pixel at col, row white
        row=row+1
    col=2
   
row =1
    while row<100:
        # color pixel at col,row white
        row=row+1

    # ...

We could do this 100 times for each column, but this is clearly a place where we should iterate  through the columns. So we set up a nested loop. The outer loop walks through each column, the inner loop walks through each row:

    col=1
    while col<100:
        row =1
        while row<100:
            # color pixel col, row white
            row=row+1

        col=col+1

Now let's write some specific code which draws a white rectangle in the top-left corner:
 
    name=pickAFile()
    pic=makePicture(name)
    show(pic)
    color=makeColor(255,255,255)  # white
    col=1
    while  col<100:
        row=1
        while row < 100:
            pixel=getPixel(pic,col,row)
            setColor(pixel,color)

            row=row+1
        col=col+1
        repaint(pic)


 
You can also look at and change just the R, G, or B value of a pixel using the set/get color functions:
 
   getRed(pixel)           setRed(pixel,value)
   getGreen
(pixel)       setGreen(pixel,value)
   getBlue
(pixel)          setBlue(pixel,value)
   
Changing a color of a pixel is something like an x=x+1 operation. For instance, the following will redden a pixel:

    red = getRed(pixel)  
    setRed(pixel,red+20) # be careful here for overflow
 
After making changes to a pic, you need to redraw it to see it:
 
    repaint(pic)  #gets the system to redraw the picture.
 
Note that the sample program above manipulates a fixed box within the image. But often you'll want to manipulate the entire height and width of a picture. The following functions can help:
 
    getWidth(pic)  ->  this returns the width of the image.
    getHeight(pic) ->  this returns the height of the image
 
Using these methods allows you to process an entire image pixel-by-pixel. The loop template for this is:
 
# **** LOOP TEMPLATE ****
    col=1
    while col <getWidth(pic):
        row=1
        while row < getHeight(pic):
             pixel=getPixel(pic,col,row)
             # do something to pixel
             row=row+1
        col=col+1
 
Be very careful with your indentation!
 
You’ll use loops like the above many times as you write media programs.

In-Class Assignment

1. Type in the ‘white rectangle’ code above into whiteRectangle.py above and get it to run.
2. Write a JES program ‘blueThirdColumns.py’ that draws every third column of an image blue. Start with the whiteRectangle code above.
Hint: you'll need to use the getWidth and getHeight functions.
3. Write the program 'greenFourthRows.py' so that it draws every fourth row green.
4. Paste the generated pictures and the corresponding code for these programs onto your portfolio.


Recent site activity