Book Image

Python 2.6 Graphics Cookbook

By : Michael J Ohlson
Book Image

Python 2.6 Graphics Cookbook

By: Michael J Ohlson

Overview of this book

Python is a great object-oriented and interactive programming language that lets you develop graphics, both static and animated, using built-in vector graphics functions that are provided with Python. Python 2.6 Graphics Cookbook is a collection of straightforward recipes and illustrative screenshots for creating and animating graphic objects using the Python language. This book makes the process of developing graphics interesting and entertaining by working in a graphic workspace without the burden of mastering complicated language definitions and opaque examples. If you choose to work through all the recipes from the beginning, you will learn to install Python and create basic programs for making lines and shapes using the built-in Tkinter module. The confusing topic of color manipulation is explored in detail using existing Python tools as well as some new tools in the recipes. Next you will learn to manipulate font size, color, and placement of text as placing text exactly where you want on a screen can be tricky because font height, inter-character spacing, and text window dimensions all interfere with each other. Then you will learn how to animate graphics, for example having more than one independent graphic object co-exist and interact using various Python methods. You will also learn how you can work with raster images, such as converting their formats using the Python Imaging Library. Next you will learn how you can combine vector images with raster images so that you can animate the raster images with ease. You will also walk through a set of recipes with the help of which you can handle and manipulate blocks of raw data that may be hundreds of megabytes in size using datastreams, files, and hard drives. You will also learn how you can use Inkscape to dismantle existing images and use parts of them for your own graphics and Python programs. At the end of the book you will learn how you can create GUIs for different purposes.
Table of Contents (18 chapters)
Python 2.6 Graphics Cookbook
Credits
About the Author
About the Reviewers
Preface
Index

Running a shortest Python program


We need a one line Python program that will prove that the Python interpreter is installed and working on our computer platform.

How to do it...

  1. Create a folder (directory) called something like construction_work or constr for short. You will place all your Python programs inside this directory. In a text editor such as gedit on Linux or notepad on Windows. If we are working in Windows, there is a nice editor called "Context" that can be downloaded for free from http://www.contexteditor.org/ Context, that is sensitive to Python syntax and has a search-and-replace function that is useful.

  2. Type the following line:

    Print 'My wereld is "my world" in Dutch'
  3. Save this as a file named simple_1.py, inside the directory called constr.

  4. Open up an X terminal or a DOS window if you are using MS Windows.

  5. Change directory into constr - where simple_1.py is located.

  6. Type python simple_1.py and your program will execute. The result should look like the following screenshot:

  7. This proves that your Python interpreter works, your editor works, and that you understand all that is needed to run all the programs in this book. Congratulations.

    """
    Program name: simplest_1.py
    Objective: Print text to the screen.
    
    Keywords: text, simplest
    =========================
    Printed "mywereld" on terminal.
    Author:          Mike Ohlson de Fine
    
    """
    Print 'mywereld is "my world" in Dutch'

How it works...

Any instructions you type into a Linux X terminal or DOS terminal in MS Windows are treated as operating system commands. By starting these commands from within the same directory where your Python program is stored you do not have to tell the Python and operating system where to search for your code. You could store the code in another directory but you would then need to precede the program name with the path.

There's more...

Try the longer version of the same basic print instructions shown in the following program.

All the text between the """ (triple quotation marks) is purely for the sake of good documentation and record keeping. It is for the use of programmers, and that includes you. Alas, the human memory is imperfect. Bitter experience will persuade you that it is wise to provide fairly complete information as a header in your programs as well as comments inside the program.

However, in the interest of saving space and avoiding distractions, these header comments have been left out in the rest of this book.