Chapter 20. Py Art

Well, art is art, isn’t it? Still, on the other hand, water is water! And east is east and west is west, and if you take cranberries and stew them like applesauce, they taste much more like prunes than rhubarb does.

Groucho Marx

This chapter and the next two discuss the application of Python to some common human endeavors: art, business, and science. If you’re interested in any of these areas, you may get some helpful ideas or the urge to try something new.

2-D Graphics

All computer languages have been applied to computer graphics to some degree. Many of the heavy-duty platforms in this chapter were written in C or C++ for speed, but added Python libraries for productivity. Let’s begin by looking at some 2-D imaging libraries.

Standard Library

Only a few image-related modules are in the standard library:

imghdr

Detects the file type of some image files.

colorsys

Converts colors between various systems: RGB, YIQ, HSV, and HLS.

If you downloaded the O’Reilly logo to a local file called oreilly.png, you could run this:

>>> import imghdr
>>> imghdr.what('oreilly.png')
'png'

Another standard library is turtle—“Turtle graphics,” which is sometimes used to teach programming to young people. You can run a demo with this command:

$ python -m turtledemo

Figure 20-1 shows a screenshot of its rosette example.

inp2 2001
Figure 20-1. Image from turtledemo

To do anything ...

Get Introducing Python, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.