You are previewing Python 2.6 Graphics Cookbook.
O'Reilly logo
Python 2.6 Graphics Cookbook

Book Description

Learn how to use Python’s built-in graphics capabilities to create static and animated graphics for a range of real-world purposes. Over 100 recipes take you from basic shape creation to developing interactive GUIs.

  • Create captivating graphics with ease and bring them to life using Python

  • Apply effects to your graphics using powerful Python methods

  • Develop vector as well as raster graphics and combine them to create wonders in the animation world

  • Create interactive GUIs to make your creation of graphics simpler

  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to accomplish the task of creation and animation of graphics as efficiently as possible

  • In Detail

    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.

    A quick reference for creating interesting graphic animations using Python programming

    Table of Contents

    1. Python 2.6 Graphics Cookbook
      1. Python 2.6 Graphics Cookbook
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Errata
          2. Piracy
          3. Questions
      6. 1. Start your Engines
        1. Introduction
        2. Running a shortest Python program
          1. How to do it...
            1. How it works...
            2. There's more...
          2. Ensuring that the Python modules are present
            1. How to do it...
              1. How it works...
              2. There's more...
            2. A basic Tkinter program
              1. How to do it...
                1. How it works...
              2. Make a compiled executable under Windows and Linux
                1. Getting ready
                2. How it works...
      7. 2. Drawing Fundamental Shapes
        1. Introduction
        2. A straight line and the coordinate system
          1. How to do it...
          2. How it works...
          3. There's more...
        3. Draw a dashed line
          1. How to do it...
          2. How it works...
          3. There's more...
        4. Lines of varying styles with arrows and endcaps
          1. How to do it...
          2. How it works...
          3. There's more...
        5. A two segment line with a sharp bend
          1. How to do it...
          2. How it works...
          3. There's more...
        6. A line with a curved bend
          1. How to do it...
          2. How it works...
          3. There's more...
        7. Drawing intricate shapes the curly vine
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. There's more...
        8. Draw a rectangle
          1. How to do it...
          2. How it works...
          3. There's more...
        9. Draw overlapping rectangles
          1. How to do it...
          2. How it works...
          3. There's more...
        10. Draw concentric squares
          1. How to do it...
          2. How it works...
        11. A circle from an oval
          1. How to do it...
          2. How it works...
          3. There's more...
        12. A circle from an arc
          1. How to do it...
          2. How it works...
          3. There's more...
        13. Three arc ellipses
          1. How to do it...
          2. How it works...
          3. There's more...
        14. Polygons
          1. How to do it...
          2. How it works...
        15. A star polygon
          1. How to do it...
          2. How it works...
        16. Cloning and resizing stars
          1. How to do it...
          2. How it works...
          3. There's more...
      8. 3. Handling Text
        1. Introduction
        2. Simple text
          1. How to do it...
            1. How it works...
            2. There's more...
          2. Text font type, size, and color
            1. Getting ready
            2. How to do it...
            3. How it works...
            4. There's more...
            5. Getting ready
            6. How to do it...
            7. How it works...
          3. Alignment of text left and right justify
            1. Getting ready
            2. How to do it...
            3. How it works...
          4. All the fonts available on your computer
            1. How to do it...
            2. How it works...
      9. 4. Animation Principles
        1. Introduction
        2. Static shifting of a ball
          1. How to do it...
          2. How it works...
          3. There's more...
        3. Time-controlled shifting of a ball
          1. How to do it...
          2. How it works...
          3. There's more...
            1. The robustness of Tkinter
        4. Complete animation using draw-move-pause-erase cycles
          1. How to do it...
          2. How it works...
          3. There's more...
        5. More than one moving object
          1. How to do it...
          2. How it works...
          3. There's more...
        6. A ball that bounces
          1. How to do it...
          2. How it works...
        7. Bouncing in a gravity field
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        8. Precise collisions using floating point numbers
          1. How to do it...
          2. How it works...
          3. A graphic debugging tool...
        9. Trajectory tracing and ball-to-ball collisions
          1. How to do it...
          2. How it works...
          3. There's more...
          4. Why do we sometimes get tkinter.TckErrors?
        10. Rotating line
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Trajectory tracing on multiple line rotations
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        12. A rose for you
          1. How it works...
      10. 5. The Magic of Color
        1. Introduction
        2. A limited palette of named colors
          1. How to do it...
          2. How it works...
          3. There's more...
            1. To get fine shadings of the primary colors
            2. A more compact color list
        3. Nine ways of specifying color
          1. How to do it...
          2. How it works...
          3. Converting color tuples to Tkinter Hex compatible specifiers
        4. A red beachball of varying hue
          1. How to do it...
          2. How it works...
          3. There's more...
        5. A red color wedge of graded hue
          1. How to do it...
          2. How it works...
          3. There's more...
        6. Newton's grand wheel of color mixing
          1. How to do it...
          2. How it works...
          3. There's more...
        7. The numerical color mixing matching palette
          1. How to do it...
          2. How it works...
          3. There's more...
            1. There are other tools to select colors
            2. Is there a way to make neater slide controllers?
        8. The animated graded color wheel
          1. How to do it...
          2. How it works...
        9. Tkinter's own color picker-mixer
          1. How to do it...
          2. How it works...
          3. There's more...
      11. 6. Working with Pictures
        1. Opening an image file and discovering its attributes
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Things we need to know about image formats
            2. Images and the numbers game
        2. Open, view, and save an image in a different file format
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Image format conversion for JPEG, PNG, TIFF, GIF, BMP
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Does size count?
        4. Image rotation in the plane of the image
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Image size alteration
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. How do we preserve the correct height-to-width ratio of an image?
        6. Correct proportion image resizing
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Separating one color band in an image
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Red, green, and blue color alteration in images
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Slider controlled color manipulation
          1. Getting ready
          2. How to do it...
          3. How it works...
        10. Combining images by blending
          1. Getting ready
          2. How to do it...
          3. There's more...
            1. More Info Section 1
        11. Blending images by varying percentages
          1. How to do it...
          2. How it works...
          3. There's more...
        12. Make a composite image using a mask image
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        13. Offset (roll) image horizontally and vertically
          1. Getting ready
          2. How to do it...
          3. How it works...
        14. Flip horizontally, vertically, and rotate
          1. Getting ready
          2. How to do it...
          3. How it works...
        15. Filter effects: blur, sharpen, contrast, and so on
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. Getting ready
          5. How to do it...
          6. How it works...
          7. There's more...
      12. 7. Combining Raster and Vector Pictures
        1. Simple animation of a GIF beach ball
          1. Getting ready
          2. How to do it...
          3. How it works...
        2. The vector walking creature
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. More Info Section 1
        3. Bird with shoes walking in the Karroo
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Making GIF images with transparent backgrounds using GIMP
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Diplomat walking at the palace
          1. Getting ready
          2. How to do it...
          3. How it works...
        6. Spider in the forest
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Moving band of images
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Continuous band of images
          1. Getting ready
          2. How to do it...
          3. How it works...
        9. Endless background
          1. Getting ready
          2. How to do it...
          3. How it works...
      13. 8. Data In and Data Out
        1. Introduction
        2. Creation of a new file on a hard drive
          1. How to do it...
          2. How it works...
          3. How to read the newly created file
        3. Writing data to a newly-created file
          1. How it works...
        4. Writing data to multiple files
          1. How it works...
        5. Adding data to existing files
          1. How it works...
          2. So remember the difference between write and append
        6. Saving a Tkinter-drawing shape to disk
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. Storing commands
        7. Retrieving Python data from disk storage
          1. Getting ready
          2. How it works...
        8. Simple mouse input
          1. How it works...
          2. There's more...
        9. Storing and retrieving a mouse-drawn shape
          1. Getting ready
          2. How it works...
          3. There's more...
        10. A mouse-line editor
          1. Getting ready
          2. How it works...
          3. There's more...
            1. Why don't we add more features?
            2. Using other tools to acquire and re-work images
            3. How to exploit that mouse
            4. We can measure the distance along a meandering line
        11. All possible mouse actions
          1. How to do it...
          2. How it works...
          3. There's more...
      14. 9. Exchanging Inkscape SVG Drawings with Tkinter Shapes
        1. Introduction
        2. The structure of an SVG drawing
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. SVG code for separate paths
        3. Tracing the shape of an image in Inkscape
          1. Getting ready
          2. How to do it...
          3. How often do we need to click the mouse?
          4. Another way to get SVG paths from raster images
        4. Converting an SVG path into a Tkinter Line
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. How far should we go with image conversion code?
            2. Another way to get SVG paths from raster images
      15. 10. GUI Construction: Part 1
        1. Introduction
        2. Widget configuration a label
          1. How to do it...
          2. How it works...
          3. There's more...
        3. Button focus
          1. How to do it...
          2. How it works...
          3. There's more...
        4. The simplest push button with validation
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Buttons behave differently on Windows
        5. A data entry box
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Did we keep things simple?
            2. Single-line versus multi-line entry
            3. The Clever Geometry Manager
        6. Colored button causing a message pop-up
          1. How to do it...
          2. How it works...
        7. Complex interaction between buttons
          1. How to do it...
          2. How it works...
        8. Images on buttons and button packing
          1. How to do it...
          2. How it works...
          3. There's more...
        9. Grid Geometry Manager and button arrays
          1. How to do it...
          2. How it works...
          3. There's more...
        10. Drop-down menus to select from a list
          1. How to do it...
          2. How it works...
        11. Listbox variable selection
          1. How to do it...
          2. How it works...
        12. Text in a window
          1. How to do it...
          2. How it works...
      16. 11. GUI Construction: Part 2
        1. Introduction
        2. The Grid Layout Geometry Manager
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. The Pack Geometry Manager
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Radiobuttons to select one from many
          1. How to do it...
          2. How it works...
        5. Checkbuttons (Tickboxes) to select some of many
          1. How to do it...
          2. How it works...
        6. Key-stroke event handling
          1. How to do it...
          2. How it works...
        7. Scrollbar
          1. How to do it...
          2. How it works...
          3. There's more...
        8. Custom DIY controller widgets
          1. How to do it...
          2. How it works...
          3. There's more...
        9. Organizing widgets inside frames
          1. How to do it...
          2. How it works...
          3. There's more...
      17. appA. Quick tips for running Python programs in Microsoft Windows
        1. Running Python programs in Microsoft Windows
        2. Where will we find the windows installer?
        3. Do we have to use Python version 2.7?
        4. Why do we get "python is not recognized…"?