Python 2.6图形编程实战指南

需积分: 10 2 下载量 45 浏览量 更新于2024-07-18 收藏 5.93MB PDF 举报
"Python 2.6 Graphics Cookbook 是一本专注于使用Python 2.6进行图形创建和动画制作的书籍,提供超过100个实用的图形编程食谱。作者是Mike Ohlson de Fine,由Packt Publishing出版。本书旨在帮助读者通过Python 2.6版本实现各种视觉效果和动态图形,内容涵盖了图形设计的基础到高级技巧。" 在Python 2.6 Graphics Cookbook中,读者可以学习到如何利用Python的强大功能来创建引人注目的图形。Python作为一个强大的编程语言,提供了多种库用于图形处理,如matplotlib、PIL(Python Imaging Library)和pygame等。这些库可以帮助开发者生成静态图表、动态动画,甚至进行游戏开发。 1. Matplotlib库:Matplotlib是Python中最常用的绘图库,支持创建各种2D和3D图表,如线图、散点图、柱状图等。书中可能详细介绍了如何使用matplotlib创建美观的数据可视化图形,包括自定义颜色、标签、图例和轴样式。 2. Pygame:Pygame是一个Python模块,专门用于开发游戏和多媒体应用程序。它包含了许多子模块,如音频处理、事件管理、图像操作等。在Python 2.6 Graphics Cookbook中,可能会讲解如何使用pygame创建游戏界面、处理用户输入和实现基本的游戏逻辑。 3. PIL:Python Imaging Library允许开发者处理图像,包括读取、修改和保存多种图像文件格式。书中的食谱可能涵盖图像的打开、裁剪、旋转、调整大小以及色彩转换等操作。 4. 动画制作:Python 2.6 Graphics Cookbook还可能涉及如何使用Python制作动画,包括帧动画和基于时间的动画。读者将学习到如何控制图形随着时间变化,创建动态效果,比如物体的移动、旋转和缩放。 5. GUI图形界面:除了基本的图形绘制,书中可能还会介绍如何结合tkinter或wxPython等库构建图形用户界面,使用户能够与图形进行交互。 6. 数据可视化:Python在数据科学领域有广泛的应用,因此这本书可能会讲解如何将数据转化为可视化的图形,帮助理解复杂的数据模式和趋势。 7. 实例解析:为了帮助读者更好地理解和应用这些知识,书中很可能包含大量的实例代码,从简单的图形绘制到复杂的动画序列,逐步引导读者掌握Python图形编程。 请注意,由于该书已出版于2010年,因此针对的是Python 2.6版本。Python 2系列已在2020年停止维护,现在推荐使用Python 3.x版本进行开发。尽管如此,Python 2.6 Graphics Cookbook中的许多概念和技术在Python 3中依然适用,只是语法和库的使用可能需要根据Python 3进行相应的调整。对于想要了解Python图形编程基础的读者来说,这本书仍然具有一定的参考价值。
2019-08-18 上传
Preface 1 Chapter 1: Start your Engines 5 Introduction 5 Running a shortest Python program 6 Ensuring that the Python modules are present 7 A basic Tkinter program 9 Make a compiled executable under Windows and Linux 11 Chapter 2: Drawing Fundamental Shapes 15 Introduction 16 A straight line and the coordinate system 17 Draw a dashed line 18 Lines of varying styles with arrows and endcaps 20 A two segment line with a sharp bend 22 A line with a curved bend 23 Drawing intricate shapes – the curly vine 24 Draw a rectangle 27 Draw overlapping rectangles 28 Draw concentric squares 30 A circle from an oval 32 A circle from an arc 34 Three arc ellipses 35 Polygons 36 A star polygon 37 Cloning and resizing stars 39 Chapter 3: Handling Text 43 Introduction 43 Simple text 43 ii Table of Contents Text font type, size, and color 45 Alignment of text – left and right justify 49 All the fonts available on your computer 54 Chapter 4: Animation Principles 57 Introduction 57 Static shifting of a ball 58 Time-controlled shifting of a ball 59 Complete animation using draw-move-pause-erase cycles 62 More than one moving object 63 A ball that bounces 65 Bouncing in a gravity field 67 Precise collisions using floating point numbers 70 Trajectory tracing and ball-to-ball collisions 72 Rotating line 76 Trajectory tracing on multiple line rotations 78 A rose for you 82 Chapter 5: The Magic of Color 85 Introduction 85 A limited palette of named colors 86 Nine ways of specifying color 90 A red beachball of varying hue 91 A red color wedge of graded hue 94 Newton's grand wheel of color mixing 96 The numerical color mixing matching palette 101 The animated graded color wheel 106 Tkinter's own color picker-mixer 110 Chapter 6: Working with Pictures 113 Opening an image file and discovering its attributes 114 Open, view, and save an image in a different file format 117 Image format conversion for JPEG, PNG, TIFF, GIF, BMP 118 Image rotation in the plane of the image 120 Image size alteration 121 Correct proportion image resizing 123 Separating one color band in an image 124 Red, green, and blue color alteration in images 125 Slider controlled color manipulation 127 Combining images by blending 130 Blending images by varying percentages 131 Make a composite image using a mask image 132 iii Table of Contents Offset (roll) image horizontally and vertically 134 Flip horizontally, vertically, and rotate 134 Filter effects: blur, sharpen, contrast, and so on 135 Chapter 7: Combining Raster and Vector Pictures 139 Simple animation of a GIF beach ball 140 The vector walking creature 141 Bird with shoes walking in the Karroo 145 Making GIF images with transparent backgrounds using GIMP 149 Diplomat walking at the palace 152 Spider in the forest 156 Moving band of images 160 Continuous band of images 162 Endless background 164 Chapter 8: Data In and Data Out 167 Introduction 167 Creation of a new file on a hard drive 168 Writing data to a newly-created file 169 Writing data to multiple files 169 Adding data to existing files 170 Saving a Tkinter-drawing shape to disk 171 Retrieving Python data from disk storage 172 Simple mouse input 173 Storing and retrieving a mouse-drawn shape 174 A mouse-line editor 177 All possible mouse actions 181 Chapter 9: Exchanging Inkscape SVG Drawings with Tkinter Shapes 185 Introduction 185 The structure of an SVG drawing 186 Tracing the shape of an image in Inkscape 189 Converting an SVG path into a Tkinter Line 194 Chapter 10: GUI Construction: Part 1 199 Introduction 199 Widget configuration – a label 200 Button focus 201 The simplest push button with validation 203 A data entry box 204 Colored button causing a message pop-up 207 Complex interaction between buttons 208 Images on buttons and button packing 211 iv Table of Contents Grid Geometry Manager and button arrays 213 Drop-down menus to select from a list 215 Listbox variable selection 216 Text in a window 218 Chapter 11: GUI Construction: Part 2 219 Introduction 219 The Grid Layout Geometry Manager 220 The Pack Geometry Manager 222 Radiobuttons to select one from many 223 Checkbuttons (Tickboxes) to select some of many 224 Key-stroke event handling 226 Scrollbar 227 Custom DIY controller widgets 228 Organizing widgets inside frames 232 Appendix: Quick tips for running Python programs in Microsoft Windows 235 Running Python programs in Microsoft Windows 235 Where will we find the windows installer? 235 Do we have to use Python version 2.7? 236 Why do we get "python is not recognized…"? 236 Index 239 Preface 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. What this book covers Chapter 1, Start your Engines: This chapter explains how to acquire and install the Python interpreter, for MS Windows or Linux as well as how to verify that Python is correctly installed. This chapter explains how to create complete working programs that can be run on client computers that do not have Python installed. Chapter 2, Drawing Fundamental Shapes: This shows how to create all the fundamental graphic elements including lines, circles, ovals, rectangles, polygons, and complex curves. Simple examples are provided to demonstrate how to draw the elementary shapes. The examples also provide a ready for reference for later use. Chapter 3, Handling Text: This chapter demonstrates how to control font size, color, and position using any of the font typefaces installed on the specific operating system being used. A simple means of discovering and demonstrating all available fonts on the operating system is shown. Chapter 4, Animation Principles: This chapter starts with examples of simple sequences of a circle in different positions and systematically progresses to smoothly-moving animations of elastic balls bouncing inside a gravity field. Chapter 5, The Magic of Color: This chapter begins with the assembling of color palettes using color names recognizable to Python. The way colors are constructed using numbers to mix controlled amounts of red, green, and blue is explained. Tools for matching colors to any sample are constructed. This chapter demonstrates how to vary shadings of one color into another. Download from Wow! eBook Preface Chapter 6, Working with Pictures: This chapter reveals how to acquire and use the Python Imaging Library to manipulate photo images. It also shows methods of image format conversion, re-sizing, rotating, color transforming, and complex filtering. Chapter 7, Combining Vector and Raster Images: This chapter demonstrates the ways of combining animated vector graphics with photographic images to produce complex animations. Chapter 8, Data in and Data Out: This chapter starts with basic storing and retrieving of files to a hard drive and progresses to the construction of programs that are tools for creating, storing, and retrieving free-form shapes drawn using a mouse. Chapter 9, Exchanging Inkscape SVG Drawings with Tkinter Shapes: This chapter shows in detail how to use the Inkscape drawing tool to convert shapes traced from a photographic image into a sequence of points which reproduce the shape in Python. Once a line is expressed as a Python sequence, it can be transformed numerically in many ways. Chapter 10, GUI Construction: Part 1: This chapter provides basic examples of how to create buttons, data entry boxes, drop-down menus, list-boxes, and text labels. It also covers how to customize button appearance. Chapter 11, GUI Construction: Part 2: Here the Grid Layout Manager and the Pack Layout Manager are explained and demonstrated. Examples of radio buttons, check buttons, scrollbars, frames, and keystroke event coding are given. It also shows how to construct widgets using graphic elements on a canvas. Appendix, Quick tips for running Python programs in Microsoft Windows: This gives explanations of how to overcome some of the difficulties a new python programmer might encounter when trying to use Python in Windows. What you need for this book To run the code in this book, the reader will need a Linux operating system or Microsoft Windows, and some way of downloading Python, the Python Imaging Library, and Inkscape from the internet. All these applications are free and open source. The code has been developed on Linux Ubuntu version 9.04, Microsoft Windows XP, and Windows 7. Who this book is for This book is for Python programmers wanting simple, clear examples of graphic programming using Python. The examples are aimed at anyone wanting to use graphic elements and images inside Python programs with the minimum of complexity. The intended reader ranges from scholars and teachers to engineers and technicians