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

Make a compiled executable under Windows and Linux


How do we create and execute a.exe file that will run a compiled version of our Python and Tkinter programs? Can we make a self-contained folder that will run on an MS Windows or Linux distribution that uses a different version of Python from the ones we use? The answers to both questions are yes and the tool to achieve this is an Open Source program called cx_Freeze. Often what we would like to do is have our working Python program on a memory stick or downloadable on a network and be able to demonstrate it to friends, colleagues, or clients without the need to download Python onto the client's system. cx_Freeze allows us to create a distributable form of our Python graphic program.

Getting ready

You will need to download cx_Freeze from http://cx-freeze.sourceforge.net/. We need to pick a version that has the same version number as the Python version we are using. Currently, there are versions available from version 2.4 up to 3.1.

How to do it under MS Windows...

  1. MS Windows : Download cx_Freeze-4.2.win32-py2.6.msi, the windows installer for Python 2.6. If we have another Python version, then we must choose the appropriate installer from http://cx-freeze.sourceforge.net/.

  2. Save and run this installer.

  3. On completion of a successful Windows install we will see a folder named cx_Freeze inside \Python26\Lib\site-packages\.

How to do it under Linux (Debian and Ubuntu)...

  1. In a terminal run the command apt-get install cx-freeze.

  2. If this does not work we may need to first install a development-capable version of Python by running the command apt-get install python-dev. Then go back and repeat step 1.

  3. Test for success by typing in python in a command terminal to invoke the Python interpreter.

  4. Then after the >>> prompt, type import cx_Freeze. If the interpreter returns a new line and the >>> prompt again, without any complaints, we have been successful.

How to compile under both Linux and MS Windows...

  1. If the file we want to package as an executable is named walking_birdy_1.py in a folder called /constr, then we prepare a special setup file as follows.

    #setup.py
    from cx_Freeze import setup, Executable
    setup(executables=[Executable("/constr/walking_birdy_1.py")])
  2. Save it as setup.py.

  3. Then, in a command terminal run

    python /constr/setup.py build
  4. We will see a lot of system compilation commands scrolling down the command terminal that will eventually stop without error messages.

  5. We will find our complete self-contained executable inside a folder named build. Under Linux, we will find it inside our home directory under /build/exe.linux-i686-2.6. Under MS Windows, we will find it inside C:\Python26\build\exe.win-py2.6.

  6. We just need to copy the folder build with all its contents to wherever we want to run our self-contained program.

How it works...

A word of caution. If we use external files like images inside our code, then the path addresses of the files must be absolute because they are coded into, or frozen, into the executable version of our Python program. There are ways of setting up search paths which can be read at http://cx-freeze.sourceforge.net/cx_Freeze.html.

For example, say we want to use some GIF images in our program and then demonstrate them on other computers. First we place a folder called, for example, /birdy_pics, onto a USB memory stick. In the original program, walking_birdy_1.py, make sure the path addresses to the images point to the /birdy_pics folder on the stick. After compilation, copy the folder build onto the USB memory stick. Now when we double-click on the executable walking_birdy_1 it can locate the images on the USB memory stick when it needs to. These files include everything that is needed for your program, and you should distribute the whole directory contents to any user who wants to run your program without needing to install Python or Tkinter.

What about py2exe?

There is another program called py2exe that will also create executables to run on MS Windows. However, it cannot create self-contained binary executables to run under Linux whereas cx_Freeze can.