Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

In this assignment we got to know some methods for modifying image files using python. To manipulate each pixel of an image, the basic algorithm we used was a pair of for loops: one looping over each row of pixels, the second to loop over each pixel in that row. With this pair of loops we can access each pixel and manipulate it accordingly.

Our first task was to make a function that takes an image, in the form a pixmap, and applies it to a second image in a location we specify. A pixmap is a way python can store an image in a way that is readable to it. The ability to convert an image to a pixmap, as well as many other functions, are part of the graphics library in python. So the basic structure of this function is to loop through the pixels of the first pixmap, and at each pixel location copy the pixel and apply it to the second pixmap, at the location given by the function parameters, x and y, added vector-wise to the coordinates of the pixel in the source pixmap.

Our next task was to take an image and filter its colors to appear almost like a silk screen print. To achieve this we again simply looped through each pixel of a pixmap. At each pixel location we stored the rgb values to a tuple, and then reassigned them, eg switching the first and last elements of the tuple to switch red and green for each other. I made 9 different filters this way, with different permutations of rgb.

Next we made a program that takes a source image and turns it into a Warhol-esque grid of differently filtered images. Here's my end result:
My warholizer script takes the source image, duplicates it into 9 different pixmaps, applies my 9 filter functions to each pixmap, and then use my first function to apply each filtered pixmap to a canvas.

Next I made a program that runs through the pixels of a pixmap and tests to see if they are blue. If they are, the program switches the color to red. I achieved this with an if statement which tests each pixels rgb value to see if blue is greater than red and green. If it is, the program switches the red and blue channels of that pixel. Thus I could convert an image of myself in front of a blue screen to myself in front of a red screen.

Finally I undertook an extension to put myself from the picture above into a picture of the moon. I only had to modify my code from above a little bit: instead of selecting the pixels that were blue with the if statement, I had to now select the pixels that were not blue. Then I took those selected pixels and applied them to a new image with the same x and y coordinates they had before (my images were conveniently the same size, thanks imagemagick)

In conclusion, this project showed me how for loops can make a task as grueling as sifting through millions of pixels as easy as a few lines of code. This project also helped me learn about how a computer stores colors in pixels, namely with rgb values each ranging from 0 to 255.

p.s. thank you Dan Vogel for showing me how to duplicate a pixmap.