Assignment 2. Homework - Images


Due Monday, August 22 at 1:00 pm Pacific

  • Submissions received by due date receive a small on-time bonus.
  • All students are granted a pre-approved extension or "grace period" of 0 hours after the due date. Late submissions are accepted during the grace period with no penalty.
  • The grace period expires Mon, Aug 22 at 1:00 pm Pacific, after which we cannot accept further late submissions.
  • In this course, we express all date/times in Pacific time GMT -7. Our Paperless submission system also displays/records due dates and submission times in Pacific time.

Homework - Image

For this assignment, you'll write your code for in PyCharm and run your code from the command line. To get started, download image-grid.zip. Open that .zip file to get a "image-grid" folder. Copy that folder to your computer (in Windows, confusingly, the folder may be nested inside another folder with the same name). Open the image-grid folder with the PyCharm open. menu command to get started (do not double click the image-grid.py file to get started, as that introduces some problems).


Pillow Install

For this project, you need to install the "Pillow" library. Use this line in the terminal, perhaps from within PyCharm (do not type the "%" sign – that is your "prompt").

Mac:

% python3 -m pip install Pillow

Windows:

% py -m pip install Pillow

It may mention something about your pip version, but you can ignore that.

Important Windows users note: in general, when you are told to type "python3", on Windows you should type "py", as shown in the two commands above (sometimes we'll just show the python3 example text).


main()

When a Python program runs, the special function main() is the first to run, and it calls other functions. For this program we provide a working main() function. Depending on the command line arguments, main() calls one or another of your functions as shown below.


a. Hello

Just to see that you have the command line working, run your program with the -hello flag with your name, like this (again, don't type the "%" sign):

% python3 image-grid.py -hello Alice
Everything's coming up Alice!
%

Remember: on Windows it's "python" or "py" instead of "python3". The easiest way to get a command line in your image-grid directory is probably the "terminal" tab at the lower-left of the PyCharm window of your image-grid project.


Project - Bluescreen

For this project, you will use code and your creativity to create a bluescreen image. We will have a class art-show and some small prizes with all this artistic output.


Bluescreen Contest Categories

We will have an in class art-show contest using these images.

The contest categories for the images are:

  • Best artistic
  • Best humor
  • Best use of background
  • Best you hanging out with someone super famous

The foreground image for the bluescreen must be one that you yourself take; not something you grabbed off the internet. Often you yourself will appear in your foreground image. The Background image can be from anywhere, and please keep in mind that a selection of the entries will be shown to the whole class.


Bluescreen Code

See file bluescreen.py (in the image-grid folder) which has working code set up like the lecture monkey example. You can use adjust this code in any way, if needed, to get the effect you want. Run the front strategy with this command line.

% python3 bluescreen.py monkey-500.jpg moon-600.jpg 

With the front strategy, the back image needs to be at least as big as the front image

See the back strategy with with the following command line. In the command line below, the "200" is a shift_x applied to the monkey, so 200 moves it to the right, and -50 would move it to the left on the background image.

% python3 bluescreen.py monkey-500.jpg 200 stanford-600.jpg 

Art Show

For the grading of this project, you can get full marks from an image that demonstrates creating a real image and processing it with the bluescreen algorithm, and we will not be picky about the level of artistry or creativity. The artistry and creativity are important instead for the in-class art show and contest.

How to create your own bluescreen:


1. Take Several Foreground Images

First create several "foreground" pictures with people or whatever in front of a blue or green colored background, and save those images on your computer. You can use, say, a blue towel as background. We are not grading on the artistic details of your photography here; we just want an image and some code that work together. For the contest however, the sky is the limit.

Take a few different foreground images so you can experiment. For example, take one image with people in the center and another with them off to the side to fit in with interesting parts of the background. You can also try dressing the people in the foreground with blue clothes, so the background will show through those areas. You don't have to use blue, green is also popular, and you would just fix the code to work with your background color.


Image Size

Digital cameras create very large images like 4000 by 3000 pixels, 12 megapixels, and such large images will take too long to process and have far more pixels than the displays we have. Keep your original images, and create smaller versions for processing, say reduced to a width of 600 or 800 pixels with a filename like "myimage-600.jpg".

On the Mac, the built-in Preview application can crop and resize and export images, and on Windows I assume there's something similar. In Preview: open your original, use the Duplicate menu command first to save your original, then crop and resize and save.


2. Fix It In Post (optional)

"Fix It In Post" is a sort of Hollywood joke, referring to fixing some on-camera mistake later on in the computer post-production phase. This is totally optional, but you can do the same sort thing with your bluescreen image. For example, if part of the blue background is not quite big enough, you can edit the image in a paint program, and draw a blue rectangle over the area to just make it be blue the way you want. Since the blue gets removed by the bluescreen algorithm, it never appears in the final image. You are just creating a sort of hole for the background image to show through. On the Mac, the built-in Preview image app has a very limited ability to put colored rectangles and ovals on things, and on Windows the Paint program is similar.

You can fix up your image this way, but we want the actual bluescreen composition to be done by your code.

There are two strategies shown in the bluescreen.py file; either of these is fine for your output.


3. Bluescreen "front" Algorithm

The simplest bluescreen form is the one shown with the monkey/moon example.

Front Algorithm: Loop over the main front image (e.g. monkey in front of blue). Detect blue pixels, copy over pixels from the background on top of the blue pixels. The background needs to be as big as the front image, so we do not get out-of-bounds errors accessing non-existent pixels on the background. If you get that error, resize the back to be bigger in an image editor and try again. The changed front image is the final output


4. Alternate: Bluescreen "back" Algorithm

Loop over the front image. Detect non-blue (i.e. monkey) pixels. Copy these monkey pixels over to the back image, possibly shifted by some shift_x/shift_y amount to move the monkey to a specific spot on the background. When done, the changed background image is the final output.


5. Saving Your Output

The bluescreen output image files should be the product of your input files and your edits to the bluescreen.py code. You will submit your output image files and your bluescreen.py code. Save your favorite bluescreen output as a .png or .jpg file for upload. Please use your name in the file name, like "jane-smith1.png" to help use keep the entries organized. You must submit 1 bluescreen image, and you can submit a second if you like.


All Done

When you have your bluescreen output looking good, please submit your 1 or 2 output image files by posting them on the Ed discussion board! We'll have a thread for all your images!