''' images.py Jeff Ondich, 1 Nov 2009 This program illustrates pixel-based manipulation of images using PIL. To run this program, you need 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. You can find instructions for installing PIL on your computer at http://apps.carleton.edu/curricular/cs/resources/source/PILinstallation/. Your job for the image processing assignment is to write the functions below whose definitions currently contain only the stub code "return originalImage". Simple testing code for all the functions, including the stubs, is included at the bottom of this program. ''' import sys import Image def getGreenImage(originalImage): '''Returns a copy of the specified image with all the red and blue removed.''' greenImage = originalImage.copy() greenPixels = greenImage.load() imageWidth = greenImage.size[0] imageHeight = greenImage.size[1] for y in range(imageHeight): for x in range(imageWidth): greenValue = greenPixels[x, y][1] greenPixels[x, y] = (0, greenValue, 0) return greenImage def getSmallImage(originalImage): '''Returns a copy of the specified image, scaled to half size both horizontally and vertically.''' smallImageWidth = originalImage.size[0] / 2 smallImageHeight = originalImage.size[1] / 2 originalPixels = originalImage.load() smallImage = Image.new("RGB", (smallImageWidth, smallImageHeight)) smallImagePixels = smallImage.load() for y in range(smallImageHeight): for x in range(smallImageWidth): red = (originalPixels[2*x, 2*y][0] + originalPixels[2*x + 1, 2*y][0]\ + originalPixels[2*x, 2*y + 1][0] + originalPixels[2*x + 1, 2*y + 1][0]) / 4 green = (originalPixels[2*x, 2*y][1] + originalPixels[2*x + 1, 2*y][1]\ + originalPixels[2*x, 2*y + 1][1] + originalPixels[2*x + 1, 2*y + 1][1]) / 4 blue = (originalPixels[2*x, 2*y][2] + originalPixels[2*x + 1, 2*y][2]\ + originalPixels[2*x, 2*y + 1][2] + originalPixels[2*x + 1, 2*y + 1][2]) / 4 smallImagePixels[x, y] = (red, green, blue) return smallImage def getGrayImage(originalImage): '''Returns a copy of the specified image in black-and-white.''' return originalImage def getMirrorImage(originalImage): '''Returns a copy of the specified image, reflected horizontally (so, for example, if there were a ">" in the original image, it would appear as a "<" in the mirror image).''' return originalImage def getRotatedImage(originalImage): '''Returns a copy of the specified image, rotated clockwise by 90 degrees. (Note that you will need to create a new image whose dimensions are different than the original image's dimensions. See getSmallImage for an example of how to do this.)''' return originalImage # Main program. if __name__ == '__main__': if len(sys.argv) != 2: print 'Usage: %s imageFile' % sys.argv[0] exit() imageFileName = sys.argv[1] image = Image.open(imageFileName) image.show() greenImage = getGreenImage(image) greenImage.show() smallImage = getSmallImage(image) smallImage.show() grayImage = getGrayImage(image) grayImage.show() mirrorImage = getMirrorImage(image) mirrorImage.show() rotatedImage = getRotatedImage(image) rotatedImage.show()