Activity 1.4.2 Objects and Methods
In this lesson you will be creative with images. Starting with image files, you will load image objects into memory and then do interesting things with them. What are image files? They're data, really just zeros and ones. Images can be stored and retrieved like any other data files.
Once an image is in memory, we can perform a "simple" action on it like enlarging, brightening, or rotating it. These verbs abstract a complicated operation that can involve millions of calculations. When you move a window on the screen, click on a menu, or even just move the mouse, the pixels on the screen change. These are manipulations of images. In each case the central processing unit and the processors on the graphics card handle millions of ones and zeros to render fresh images on the monitor. How do we use objects and methods to handle these complex operations?
· Computer with Enthought Canopy distribution of Python® programming language
· Webcam or other way to obtain a digital picture
· Practice file woman.jpg and cat1-a.gif
Part I: Working with a Filesystem
1. To open an image in a program, you will need a way to find the file using the programming language. You can use a file’s absolute filename. Most operating systems and programming languages remember one location in the file system as your "working directory," and a file can be described relative to that location: a relative filename. First we deal with absolute filenames.
Most file systems are hierarchical, forming a tree that begins with a root directory. An absolute filename specifies where the file is stored from the root, which is typically indicated by / in UNIX and Mac Operating Systems and by the startup drive letter in Windows, such as C:\.
Files and directories are nodes, each branching from one parent in the tree, with the root considered the “top” of the tree. The absolute filename of admin (in the green box below) is C:/Users/admin. What is the absolute filename of nice.jpg (in the red circle below)?
2. A filename can be specified with a relative filename. A relative name means that the file location is described starting from the current working directory. It does not begin with the root / or C:\. The special symbol of two periods .. is used by many languages to represent "up one level in the tree."
If we were currently working in the admin directory, what would be the relative filename for nice.jpg?
3. The table below lists the commands from UNIX for navigating the tree. Even when you run a Python environment on Windows or another operating system, Python will recognize these UNIX commands.
Print working directory.
Change directory. By itself, it means change to the user’s home directory.
Move one level up toward the root. The double dot is an abbreviation for the directory above the current one.
Move downward in the tree into directory dirname.
List all files and directories...