further graphical programming for in range(photo width())for in range(photo height())red,green,blue pix[ ,javg (red+green+blue)// pix[ , (avgavgavgphoto=imagetk photoimage(imagecanvas create_image( , ,image=photo,anchor=nwdef load_file(filename)global imagephoto image=image open(filenameconvert'rgb 'photo=imagetk photoimage(imagecanvas configure(width=photo width()height=photo height()canvas create_image( , ,image=photo,anchor=nwroot title(filenameroot tk(button button(text'change 'font='verdana ' )command=changecanvas canvas(canvas grid(row= button grid(row= load_file'pic jpg 'mainloop(let' first look at the load_file function many of the image utilities are in the image module we give nameimageto the object created by the image open statement we also use the convert method to convert the image into rgb (red-green-blueformat we will see why in minute the next line creates an imagetk object called photo that gets drawn to the tkinter canvas the photo object has methods that allow us to get its width and height so we can size the canvas appropriately now look at the change function the image object has method called load that gives access to the individual pixels that make up the image this returns two-dimensional array of rgb values for instanceif the pixel in the upper left corner of the image is pure whitethen pix[ , will be ( , , if the next pixel to the right is pure blackpix[ , will be ( , , to convert the image to grayscalefor each pixel we take the average of its redgreenand blue componentsand reset the redgreenand blue components to all equal that average remember that if the redgreenand blue are all the samethen the color is shade of gray after modifying all the pixelswe create new imagetk object from the modified pixel data and display it on the canvas you can have lot of fun with this try modifying the change function for instanceif we use the following line in the change functionwe get an effect that looks like photo negativepix[ , ( -red -green -bluetry seeing what interesting effects you can come up with notethoughthat this way of manipulating images is the slowmanual way pil has number of much faster functions for modifying images you can very easily change the brightnesshueand contrast of imagesresize themrotate themand much more see the pil reference materials for more on this