These notes describe how to write Python programs that perform Photoshop-like operations on images. Before tackling these notes, try the introductory tutorial on JES Python Media Programming.Representing Images in Digital Form or 'A Picture is worth a Thousand Pixels!'col=1 2 3 4 5
In the JES Media system, an image consists of a table of pixels. The left top corner of the image is coordinate (col=1,row=1) (not 0). Each pixel holds 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. Some Key Image FunctionsmakeColor(r,g,b) -> this returns a color, e.g.,purple=makeColor(255,0,255) # note that some colors are also built-in getPixel(pic,x,y) -> this returns a pixel from an image, e.g., pixel=getPixel(pic,5,7) setColor(pixel, color) setColor(pixel,white) getWidth(pic) --> this returns the width of a picture w = getWidth(pic) getHeight(pic) --> this returns the height of a picture w = getHeight(pic) repaint(pic) --> this redraws a picture after pixels have been changed repaint(pic) We'll use the JES functions on the previous page, and others, to create some cool images. Often, we'll use these functions within loops. In fact, we'll often use a loop within a loop. Manipulating Images Pixel-by-PixelHow would you draw a red pixel in the top-left corner?How would you draw a red pixel in the center of a picture? How would you draw a red line across the top row of a picture? How would you draw a red border of thickness three across the top of the picture? How would you draw a border of vertical length 100 across a picture? Solutions How would you draw the top-left pixel red? filename=pickAFile() pic=makePicture(filename) show(pic) pixel = getPixel(pic,1,1) setColor(pixel,red) repaint(pic) How would you draw a red pixel in the center of a picture? filename=pickAFile() pic=makePicture(filename) show(pic) w = getWidth(pic) h = getHeight(pic) pixel = getPixel(pic,w/2,h/2) setColor(pixel,red) repaint(pic) How would you draw a red line across the top row of a picture? filename=pickAFile() pic=makePicture(filename) show(pic) w = getWidth(pic) col=1 while col<w: pixel = getPixel(pic,col,1) setColor(pixel,red) col=col+1 repaint(pic) How would you draw a red border of thickness three across the top of the picture? filename=pickAFile() pic=makePicture(filename) show(pic) w = getWidth(pic) col=1 while col<w: pixel = getPixel(pic,col,1) setColor(pixel,red) pixel = getPixel(pic,col,2) setColor(pixel,red) pixel = getPixel(pic,col,3) setColor(pixel,red) col=col+1 repaint(pic) In the above example, we explicitly draw the three rows with three separate setColor calls. But anytime we do something more than once, we should think 'loop'. That way you are not limited if the number of rows gets large. In this case, we must think 'nested loop'. How would you draw a border of vertical length 100 across a picture? filename=pickAFile() pic=makePicture(filename) show(pic) w = getWidth(pic) col=1 while col<w: row=1 while row<100: pixel = getPixel(pic,col,row) setColor(pixel,red) row=row+1 col=col+1 repaint(pic) 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. We then get the next column, and work on its first 100 rows. The outer loop walks through the columns. The inner loop walks through the rows. In-Class Assignment1. Copy the red border code above (the last example) into a file redBorder.py and get it to run in the JES system.2. Write a JES program ‘blueThirdColumns.py’ that draws every third column of an image blue. Start with the redBorder.py 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. |