Now that we have seen the basic features of modules, objects, and methods, let's look at how they can solve real-world problems. For our running example, we will write some programs that display and manipulate pictures and other images.

Suppose you have a file called pic207.jpg on your hard drive and want to display it on your screen. You could double-click to open it, but what does that actually do? To start to answer that question, type the following into a Python prompt:

Download modules/open_pic.cmd

>>> import media >>> f = media.choose_file() >>> pic = media.load_picture(f) >>>

When the file dialog box opens, navigate to pic207.jpg. The result should be the awesomely cute photo shown in Figure 4.5, on the following page. Here's what the commands shown earlier actually did:

1. Import the functions from the media module.

2. Call that module's choose_file function to open a file-choosing dialog box. This call returns a string that contains the path to the picture file.

3. Call the module's load_picture function to read the contents of the picture file into memory. This creates a Python object, which is assigned to the variable pic.

4. Call that module's show function, which launches another program to display the picture. Python has to launch another program because it can't print the picture out at the command line.

Double-clicking would definitely have been easier.

Python Programming Madeleine
Figure 4.5: Madeleine

But let's see your mouse do this:

Download modules/pic_props.cmd

The first two commands tell us how wide and high the picture is in pixels. The third tells us the path to the file containing the picture.

Now try this:

Download modules/pic_crop.cmd

>>> media.crop_picture(pic, 150, 50, 450, 300) >>>

>>> media.save_as(pic, 'pic207cropped.jpg')

Figure 4.6: Madeleine cropped

As you can guess from the name, crop crops the picture. The upper-left corner is (150, 50), and the lower-right corner is (450, 300); the resulting picture is shown in Figure 4.6.

The code also shows the new picture and then writes it to a new file. This file is saved in the current working directory, which by default is the directory in which the program is running. On our system this happens to be '/Users/pgries/'.

Now let's put Madeleine's name on her hat. To do that, we use picture's add_text function; the result is shown in Figure 4.7, on the following page.

Download modules/pic_text.cmd

>>> media.add_text(pic, 115, 40, 'Madeleine', media.magenta) >>>

Figure 4.7: Madeleine named

Function choose_file is useful for writing interactive programs, but when we know exactly which files we want or we want more than one file, it's often easier to skip that navigation step. As an example, let's open up all three pictures of Madeleine in a single program:

Download modules/

import media picl = media.load_picture( 'pic207.jpg')

pic2 = media.load_picture( 'pic207cropped.jpg')

pic3 = media.load_picture( 'pic207named.jpg')

Since we haven't specified what directory to find the files in, the program looks for them in the current working directory. If the program can't find them there, it reports an error.




Color(0, 0, 0)


Color(255, 255, 255)


Color(255, 0, 0)


Color(0, 255, 0)


Color(0, 0, 255)


Color(255, 0, 255)


Color(255, 255, 0)


Color(0, 255, 255)


Color(255, 192, 203)


Color(128, 0, 128)

Figure 4.8: Example color values

Was this article helpful?

+1 0

Post a comment