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 Formor 'A Picture is worth a Thousand Pixels!'
An image is a matrix of pixels. A list of lists of pixels. In the JES Media system, the left top corner of the image is coordinate x=1,y=1 (the count doesn't start at 0). A pixel is a color. Colors are defined with three attributes: the amount of red, green, blue We say a Pixel is an RGB value Each of the RGB attributes are between 0 and 255 (how much data is needed to store such an attribute?) {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. Manipulating PixelsHere are some key functions provided by the JES environment: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(myPic,100,100) Generally, you'll get a pixel, then call setColor on it, as in this example: white=makeColor(255,255,255) pixel=getPixel(myPic,100,100) setColor(pixel,white) You can also 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-- 255 + 1 = 0 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 entire image. getHeight(pic) -> this returns the height of the entire image Template for manipulating each pixel of an image.row=1while row <getHeight(pic): col=1 while col < getWidth(pic): pixel=getPixel(pic,col,row) # do something to pixel col=col+1 row=row+1 Be very careful with your indentation! In-Class Assignment1. Copy/Paste the code below into a file whiteRectangle.py.name=pickAFile() pic=makePicture(name) show(pic) color=makeColor(255,255,255) # white width=100 height=100 row=1 while row<height: col=1 while col < width: pixel=getPixel(pic,col,row) setColor(pixel,color) col=col+1 row=row+1 repaint(pic) As you copy or type this program, be very careful about indentation. Note that the above program draws in a fixed part of an image (100x100). So it will only work if the image the user selects is at least that big. 2. Write a program ‘redEvenRows.py’ that draws the even rows of a picture red. Start with the whiteRectangle code above and modify it. Hint: you'll need to use the getWidth and getHeight functions. Test your program on at least two pictures. 3. Write a program greenFourthColumn.py that draws every fourth column green. |