The problems are from
Module 8
(NumPy).
Read the
attached file
Actions
and follow all instructions.
For Problem 1 you need file
animation
-transition.py
Download animation
-transition.py
and these two photos:
florida-keys-800-480.jpg
Download florida-keys-800-480.jpg
Grand_Teton-800-480.jpg
Download Grand_Teton-800-480.jpg
Watch a sample video with the required matplotlib animationDownload animation
Play media comment.
.
You also need file
image_blur.py
Download image_blur.py
with the image functions for Problem 2.
For problem 3 you will need the
code examples from the tutorial
Download code examples from the tutorial
(ZIP file) from
Module 9 (Flask).
Python Programming
Homework 6
Instructor: Dr. Ionut Cardei
The following problems are from Module 8, Introduction to NumPy.
Before starting the homework make sure you read the documents posted on the modules’ web pages.
For full credit:
Make sure you follow exactly all requirements listed in this document,
the program must have all required features and run as expected,
follow the coding style taught in the book. Write docstrings for all function contracts and comment
the tricky parts of the code. Check out the Google Python Style Guide.
Upload on Canvas: the PDF file with all solutions plus the .py source files, in this order.
First, write the solution to a problem i in in a Pyhton file pi.py, for i=1…n.
Then, paste your answers into a new Word document called h6.doc. When done, convert it to PDF and
upload the PDF version (h6.pdf) and the solution .py files on Canvas.
The PDF file is very important: the instructor will write comments on top of the PDF file on Canvas for
you to read and learn from your mistakes.
Write your full name at the top of the file and add a heading before each problem solution.
List the solutions in the PDF file in the given order, 1, 2,.…
Syntax highlighting for the code pasted in the Word file makes it more readable and helps avoid
grading mistakes. Pasting from Spyder (or another IDE) into Word may preserve the Python
colors and styles. If not, consider using http://hilite.me/ or some other online service.
The problems are missing some details (e.g. on design, some method names). Where
unspecified by requirements, feel free to make your own decisions on design and
implementation as long as they comply with the book material, coding style, and don’t
contradict the requirements. If in doubt, ask on the Homework Q&A Forum, or ask the
instructor by email.
You have unlimited attempts to upload this assignment, but only the last one before the deadline
will be graded.
IMPORTANT:
Your solution must be your own original work and it must NOT include code that was shared or
copied from somewhere else, including from ChatGPT and similar tools. Doing otherwise is against the
FAU Code of Academic Integrity.
For this assignment the instructor will use a cheating detector application that finds submissions that
indicate code shared or copied.
Name each required .py file like this: px_Lastname_Firstname.py, Substitute x with the problem
number, and Lastname, Firstname with your own last name and first name.
Example: if the problem number is 2 and your name is Jane Austen, name the problem 2 source file
p2_Austen_Jane.py.
Problem 1. Fade to Black and White
The animation-transition.py program discussed in class and posted on the Module 7 Canvas page
animates a smooth transition from one image to another and back.
Modify that program so that it displays an animation of a transition from the original color image to its
black-and-white (B/W) version and back to original. An animation video of that transition is posted on
the homework 7 page to give you the idea of how it should look like.
CAUTION: To get any credit do not cut corners, such as calling np.imread with a grayscale option, or
generating a B/W version of the color image and then using that as image 2 with no changes to the
code. Follow the requirements or expect a low grade.
Details:
a) Write a function convert_bw(img) that takes an image ndarray with shape (height,width,3) and
dtype np.uint8 and returns the B/W (actually, grayscale) version of that image, also with shape
(height,width,3) and the same dtype.
The color image pixel is represented by a shape (3,) array of np.uint8 integers: [red, green, blue], where
these numbers are in the range 0, 1, 2,…, 255. A color with equal values for red, green, and blue is a
grayscale color.
We use a trivial conversion of a pixel from [r,g,b] to B/W: the B/W pixel color is the average of the r, g,
b values. So, for each pixel [r,g,b] in the original color image we compute the B/W image pixel [a, a, a]
where a is the np.uint8 average of the red, green, and blue values.
Understand that we represent a B/W image with pixels that have all values for its colors (i.e. red, green,
and blue) equal to the average of red, green, and blue. It’s worth repeating that to avoid confusion.
Hence, the B/W image will have the same shape (height, width, 3) as the original color image.
The convert_bw function MUST use broadcasting and numpy’s functions to compute the B/W
image. Do NOT use nested for loops to compute the average for each individual pixel. If any loop is
used in this function, no credit is given for part a).
HINTS: the tricky part is converting from a (h,w) B/W array to a (h,w,3) array, where in the latter all
elements along the 3rd axis are repeated 3 times. One can use the np.dstack() function to stack arrays
on the 3rd dimension, or the np.newaxis object, as in arr[:,:, np.newaxis], to create an array with a new
axis (dimension), or reshape. Look these up.
b) Modify the image_gen() function so that it takes just one image file name and the steps as
parameters and then it uses the B/W version of the original image to yield ndarray objects that are
intermediate images between the color version and the B/W version, just like in the original py file.
c) Modify the remainder of the program accordingly in order to animate image transition from color to
B/W.
Insert the Python file into the h7.doc file.
No screenshot is needed.
Problem 2. Miniature Effect
A cool looking photographic effect is the miniature effect – diorama, or tilt-shift effect. As explained on
Wikipedia miniature faking “is a process in which a photograph of a life-size location or object is
made to look like a photograph of a miniature scale model.”
Depth of field is the “distance between the nearest and the furthest objects that give an image judged to
be in focus in a camera.”
The effect can be added with post processing to a normal photo with a wide depth of field by blurring
parts of the photograph to mimic a shallow depth of field typical for close-up photography. The result is
that the captured scene looks much smaller than in reality.
Here is an example. The original photograph with a long depth of field and no blurring is this:
Figure 1. The original photo. Notice the lack of any blurring, i.e. long depth of
field.
Notice that all parts of the picture are in focus, i.e. there is no blurring at all since the photo has a large
depth of field.
Compare it with this version of the same photo, where the top third and bottom third have been blurred
to some degree in a first attempt to produce the miniature effect:
Blurred
Unblurred
Blurred
Figure 2. Miniature photo with 3 blurring zones.
The next version of this picture, shown below, is split in 12 horizontal slices and each part has a
different degree of blurring, going from none (at the crosswalk level), then to 3×3 and up to a 31×31
blurring grid applied to the top 12th of the photo:
Figure 3. Miniature photo with 12 blurring zones.
Ideally, to accurately mimic the continuous optical phenomenon, the strength of the blurring must vary
depending on the camera angle and the actual depth of field of the subjects in the photo. We accept for
this problem the rough approximation obtained by blurring equal size horizontal slices of the
photograph with discrete levels of blurring, as seen above. We also must acknowledge that each picture
may require a different blurring map (where and how much blurring) and that each person may have
their own personal taste on how to make the effect look good.
For this problem you have to write in file p3_Lastname_Firstname.py a function
img_miniature(img, ……) that takes as parameters an image ndarray object img obtained from
calling function image_load(filename) and any other arguments you may consider necessary to
be used to customize the blurring (location and level). The img_miniature function returns a
new image ndarray of the same shape as img with the miniature effect applied to your desire,
but with minimum three slices of different blurring levels.
Write a function main() that loads a picture from a file with image_load(filename) into an object
img, applies function img_miniature(img, ……) to the loaded photo, and then saves the
resulting photograph with the miniature effect to a new file with function image_save(filename,
img).
Include in the PDF file the Python code, followed by the original photo and the photo with the
miniature effect. Use an photograph (with prior effects) of your choice. No credit is given for
the screenshots if the miniature effect is NOT applied to the original photograph.
NOTICE: To get any credit for this problem you must use the image file/save and blurring functions
from file image_blur.py, posted on the Image Effects page on Canvas and discussed in class. Do not
use any blurring functions from any other library.
Submission Instructions
Convert the h6.doc file to PDF format (file h6.pdf) and upload on Canvas the PDF file, and the Python
source files.
Rubric (100 points max. + extra credit):
Problem 1: 50 points
code correctness: 75%
coding style and following standards: 25%
Problem 2: 50 points
code correctness: 75%
coding style and following standards: 15%
2 photographs: original + miniature effect photo: 10%
IMPORTANT NOTES:
A submission that does not follow the instructions 100% (i.e. perfectly) will not get full credit.
Upload the PDF, and .py source files on Canvas.
Only submissions uploaded before the deadline will be graded.
You have unlimited attempts to upload this assignment, but only the last one uploaded before
the deadline will be graded.