'''This module defines the XImage class, which extends the PIL Image class. Jeff Ondich, 16 Feb 2007, for CS 117. For the assignment due on 21 Feb 2007, your job is to complete the toGray, reflect, tile, addFakeScratches, and medianFilter methods as described in the comments below. This program will require you to install PIL (the Python Imaging Library), which you can get from http://www.pythonware.com/products/pil/ if it's not already installed on the computer you are using. It is already installed in the CS labs in the CMC. ''' import Image import time class XImage: def __init__(self, fileName): self.imageFileName = fileName self.image = Image.open(self.imageFileName) def show(self): ''' Display this image. On some systems, the program will not continue execution until after you close the image window. On other systems, it will. ''' self.image.show() def revert(self): ''' Restore this image to the image stored in the original image file. ''' self.image = Image.open(self.imageFileName) def save(self, fileName): ''' Save this image to the file specified by the string fileName. If fileName ends in '.jpg', the image will be stored as a JPEG file. '.png', '.bmp', '.gif', '.tiff', etc. behave similarly. ''' self.image.save(fileName) def toGreen(self): ''' Removes all the red and blue from this image. ''' pixels = self.image.load() for y in range(self.image.size[1]): for x in range(self.image.size[0]): greenValue = pixels[x, y][1] pixels[x, y] = (0, greenValue, 0) def toGray(self): ''' Turns this image into a black-and-white image. That is, each pixel is changed to a shade of gray. Do this by setting the red, green, and blue values of each pixel to the average of its original red, green, and blue values. ''' pass def reflect(self): ''' Transforms this image to a left-to-right mirror image of itself. ''' pass def tile(self, squareWidth): ''' Treating this image as though it is a chess board of squares of width and height given by the integer parameter squareWidth, this method turns every other square upside-down using a top-to-bottom mirror image. Another way of thinking of it would be to superimpose a chess board on the image. Then, leave the white squares alone, but flip the black squares upside-down. ''' pass def addFakeScratches(self): ''' Adds some white pixels to an image to stand in for "scratches" on the surface of a photograph. My version of this method does diagonal white lines 50 pixels apart, but you can do anything that adds a reasonable number of unexpected white pixels to the image. (Vertical or horizontal lines are probably easier to draw, and would be perfectly acceptable.) ''' pass def medianFilter(self): ''' Applies a median filter to this image, using a 3x3 square neighborhood of each pixel. For each pixel, you will collect the set of red values in the neighborhood of the pixel, and use the median of those values as the new red value of the pixel. Do the same for the green and blue values, as discussed in class. the ''' pass if __name__ == '__main__': image = XImage('babe.jpg') image.show() s = raw_input('type something to continue') image.toGreen() image.show() s = raw_input('type something to continue') image.revert() image.toGray() image.show() s = raw_input('type something to continue') image.revert() image.reflect() image.show() s = raw_input('type something to continue') image.revert() image.tile(150) image.show() s = raw_input('type something to continue') image.revert() image.addFakeScratches() image.show() s = raw_input('type something to continue') image.medianFilter() image.show() s = raw_input('type something to continue')