1) The first line is 'import urllib', followed by 'def myCreate():' followed by the triple quote
description, as in:
import urlib
def myCreate():
"""
myCreate() function pulls a picture from the internet and creates a thumbnail
and saves it to my computer.
"""
2) You do not want to save the picture from the web in a hard coded path on your computer. I will
not be able to access it. In JES, add the line:
path = setMediaPath()
3) A Windows Explorer pop up should come up. Browse to the folder you just created to do this
assignment and press Open. The path is now set.
In my example, this would be "C:\Users\Janet\Week3\"
4) To get the picture from the web, use
data = urllib.urlretrieve(, )
In my example, I would code:
data = urllib.urlretrieve("https://www.rollingstone.com/wpcontent/uploads/2018/08/shutterstock_80072a.jpg", path + "myPicture.jpg")
This places the internet picture in my Week3 directory. You can place this picture anywhere you
want on your system.
5) Get this file by coding:
file = getMediaPath(
In my example, this would be:
file = getMediaPath("myPicture.jpg")
The first command sets the path and the second command uses the path to find your picture.
They should be used after each other in your code.
6) Make a picture by coding:
picture = makePicture(file)
show(picture)
Follow along in the text on pages 176-177 on how to make a smaller picture, ie thumbnail.
To help you scale the picture, 2 helpful methods are:
width = getWidth(picture)
height = getHeight(picture)
To create the canvas for your thumbnail:
canvas=makeEmptyPicture(int(width/4),int(height/4))
Note that the '4' is your scale, this depends on your picture size. I would go as much as '8', but if you
need something bigger, I would find another picture.
In both for loops, use 'width/scale' for target and 'length/' for target. Your thumbnail should be
in the100x100 range, a little smaller or bigger works. Too small or too big does not.
Once out of the loop, save your thumbnail as follows:
writePictureTo(canvas, "C:\Users\Janet\Downloads\half_myFile.jpg")
show(canvas)
Again, substitute your local path.
Finally, just call the function with:
"""
Pull a picture and create a thumbnail
"""
myCreate()
Save your .py file in JES by choosing File->Save Program As...
Make sure to use the .py extension on your file name.
Some programming style hints are:
1) Indent 4 spaces for each block of Python code. Don't use tabs. Using less tabs makes your
code harder to read.
2) Include the short descriptions of each class, function, method and main code in the triple quote
comments. This is called a docstring inPython. There are different styles. You can keep one line
descriptions all on a line as in:
""" This is a description. ""
•
or as I prefer, I put the triple quotes each on a separate line
"""
This is a description.
"""
Docstrings are used by various tools to automatically document the code in Python.
If this is still Greek, please get in touch.
-Please note that in order for me and other uses to use your Python file, you need to ask for the
folder path for where to put the file retrieved from the internet.
In the main code, before you call your function, ask the user for the folder path:
"""
Pull a picture and create a thumbnail
"""
userPath = requestString("Enter the folder path for the downloaded file.")
print userPath
myCreate(userPath)
In your function, change the following lines:
def myCreate(userPath):
...
data = urllib.urlretrieve(https://www.rollingstone.com/wpcontent/uploads/2018/08/shutterstock_80072a.jpg, userPath + "myPicture.jpg")
....
writePictureTo(canvas, userPath + "half_myFile.jpg")
Purchase answer to see full
attachment