TASK SUMMARY: In the fifth project of CS151, students were asked to build on their last project by altering and manipulating more aspects of ppm image files. By using new features and strategies of Python, the programmers were implored to write code to make collages, cover photos, and filters. As in the last project, the graphics, display, and sys packages were used heavily.
TASK SOLUTIONS: The first task of this project was to create a function called <buildCollage>, that would be capable of taking a list, and creating a collage from the information in this list. This was fairly complicated to accomplish. First, I had to create a destination for the collage that would be big enough to hold all of the images. By using a function (which reads the locations specified for the photo and the size of each Pixmap) created in the lab, I stored the destination Pixmap as <dst>:
Next, I defined each aspect of the list according to its index in the list. This was fairly simple and easy, as we had defined each index in lab by name, not number:
Important to this step was defining the source image as the Pixmap index from the list.
Following this, I imported several of my filters from the previous project with If and Elif statements. These tell python to execute specific filters on the Pixmap it is currently placing, and are done with the following basic pattern:
Finally, I used my new <placePixmap> function to place the altered and relocated images in the correct locations. The new version of this function allows the programmer to control the transparency, or alpha blend value, of each Pixmap. This was done with an equation given to us by the lab instructors, and involves creating new variables r2,g2, and b2 as well as rnew, bnew, and gnew in order to apply the alpha value to the pixels. The final product looks like this:
As is evident, the function uses two for loops to loop over every pixel, and then implements several graphics commands on the source image and the destination before finally applying the alpha variable.
The next task required students to create an additional function called <placePixmapNoBkg>, which would allow the collage function to remove the green screen from behind our head shots. This appeared to be fairly simple at first, but was actually not.
The first step was to loop over every pixel in the source Pixmap with two for loops, before acquiring using an if and else statement to only place those pixels that aren’t very green. The code looks like this:
In this way, I took a slightly different approach than was suggested by the instructions-- my code only alters the source image. Due to complications with other parts of my code, I could not alter the source image and send it to the destination in one single function. Instead, I created this code to remove the background, which is placed into my <buildCollage> function before the filters. By using an If statement, the programmer can decide whether or not to remove the background before any filters are added or the Pixmap is placed (this is also the 3rd task of the project). Thus, the background is removed, then the filter is applied, and then the final image is placed. This might seem slightly convoluted, but it is the best solution that the TA and I could come up with.
Here is the final code:
After doing this, I had to be sure that the lists I was inputting into the function contained the Boolean value <True>, not the string “True”. Python does not interpret them the same way, and thus cannot properly run the code.
The fourth task was to create a new file that would use the <buildCollage> function to make a collage. To do this, I first created a list of lists, with each nested list containing correctly ordered information to create the collage. Next, I used the buildCollage function on this list, which I imported from collage.py. Lastly, I wrote the following code to tell Python what to build the collage in, and how to save it to the computer:
And here is the picture of that final collage, which inputs the desired pictures from the command line with the sys package and the argv command.
The final task was to create a program that would generate a facebook-esque cover photo-- a collage at least 3 times wider than it is tall. This made it fairly easy, because it is basically the same code as the <mycollage> function. I created a collage list, used the buildCollage function on this list, and then told python where to put the Pixmaps and where to save the product. The only challenging part was figuring coordinates so that the pictures wouldn’t overlap. Here is the final image:
EXTENSIONS: I undertook two extensions for this project. The first was to enable my <placePixmapNoBkg> to remove a blue screen as well as a green screen. This was very simple: I merely had to copy the first If statement that removes green screens, and adjust the color conditions to test for very green pixels. Here is the final product, with both the green and blue conditionals included:
The second was to create new effects (filters) than I had on my last project. This was very fun, for I got some cool ideas from looking at my classmate’s solutions to these filters. I made one very simple one that doesn’t change the image at all, and three different versions of “pop art.” This changes pixels that are very green, blue, or red to separate colors, which makes impressionist painting-like images. Here they are:
As evident, I made use of the alpha value in the last one too.
WHO I WORKED WITH: On this project, I worked with the TA’s Mathias, Paige, Kellin, and Will, as well as Prashant. Additionally, I consulted the following website to find the RGB values for particularly tricky colors.
WHAT I LEARNED: This project taught me more about list organization, and the immense power they provide when transferring my ideas into python code. Additionally, I gained a better knowledge of Boolean values, as a few mistakes of mine created the need to understand them more.