Due: Tuesday, February 14, 8pm
As we've discussed, for each homework please create a new,
clearly named subfolder in your Google Drive folder (for
example, Unit 1, Lesson 3
) and put any files for the assignment in there. Please
create a new Python file
(.py
) for each homework
part and name the files accordingly. For
example unit1_hw3_part1.py
,
or if your folder is accurately named, simply name the
file part1.py
.
Ambiguously labeled homework folders or files may not
receive credit if I cannot identify which assignment they
are meant to fulfill. Thank you.
Work with user input as data for an
algorithm. Create a new file and add the Python code
from Unit 1,
Lesson 2 that finds the smallest number in a list, then
referring back to your work for the Lesson 2 Homework, modify
this so it finds the alphabetically last word in a list. Now,
at the top of the file, add import sys
, and then
use sys.argv
as the data list to run your
algorithm on. (See the
notes
on command line arguments for help.) Then, run your
program from the command line specifying 5 (or more)
words. Run the program at least two times to see the different
output. Include a screenshot of the output in your homework
folder this week, clearly labeled as "Part 1 output".
One problem you might see is that the alphabetically last word
might be the name of your Python file itself. Remember that
the first item in the sys.argv
list is the name
of the program you are running. To change this so your program
only considers the user inputs, you can remove the first item
in the list. The command to do that
is pop(0)
. (You can read
the Python
documentation on pop()
here.) So after you
import sys
, you might have the following two
lines:
word_list = sys.argv word_list.pop(0)
Allow the user to do some basic image manipulation from the command line. Create a new file for this part. Refer back to the section of the class notes that describes how to take a command line argument and open an image file. Use the version of this code the includes some error handling.
The Python Image Library
command rotate()
allows you to rotate an image within Python by specifying a
number which is degrees. Modify your error handling so it
requires two command line arguments
(i.e. len(sys.argv)
should be 3). Treat the first
as a filename and open an image file, as we have done. Then
treat the second argument as a number to pass in
to rotate()
. You will need to convert the
argument, which is always text, into a number with
the int()
command. The following line should do
it:
rotated_img = img.rotate( int(sys.argv[2]) )
The rotate()
command returns a new
image, which is why we have to create a new variable here
which I'm calling rotated_img
and assign the
result of rotate()
to that new variable.
The last thing we'll need to do is save this rotated
image. The PIL command save()
does this for
us. (Read more
documentation here.)
Save the new image we created, rotated_img
, like
this:
rotated_img.save("rotated-" + sys.argv[1])
Now try running this a couple times with different filenames and numerical values for angles to rotate by. Include a sample output in your homework folder.
Create a new file.To start, again refer back to the section of the class notes that describes how to take a command line argument and open an image file and use the version of this code the includes some error handling. Modify the error handling so that your code requires two filenames.
Open the first filename as an image as before. Copy/paste that line so your code also opens the second filename as an image.
Now let's use the Python Image Library
command blend()
. (See
the blend()
documentation for reference.) The command takes two images
and a number between 0 and 1 which corresponds to how much
transparency to use in blending them. As
with rotate()
, this returns a new image, so we'll
have to make a new variable to hold it. The line should look
something like this:
blended_img = Image.blend(img1,img2,.5)
For that line to work you will have had to name your two
image variables img1
and img2
.
Now save that new image variable. (See the save()
line in part two above, but for the filename,
use "blended.jpg"
and you don't need to
reference sys.argv
at all.)
Try running this a few times. You will need to reference images in your current directory, so if you'd like to experiment, perhaps add some additional images there. Include some sample outputs in your homework folder.
I still have not gotten a Gist from everyone. If you have not yet done so, please create a Gist and tag me in it. See the page about asking for help on the class website with instructions.