logo资料库

Python Playground 无水印pdf.pdf

第1页 / 共353页
第2页 / 共353页
第3页 / 共353页
第4页 / 共353页
第5页 / 共353页
第6页 / 共353页
第7页 / 共353页
第8页 / 共353页
资料共353页,剩余部分请下载后查看
Cover
Brief Contents
Contents in Detail
Acknowledgments
Introduction
Who Is This Book For?
What's in This Book?
Part I: Warming Up
Part II: Simulating Life
Part III: Fun with Images
Part IV: Enter the 3D
Part V: Hardware Hacking
Why Python?
Python Versions
The Code in This Book
Part I: Warming Up
1: Parsing iTunes Playlists
Anatomy of the iTunes Playlist File
Requirements
The Code
Finding Duplicates
Extracting Duplicates
Finding Tracks Common Across Multiple Playlists
Collecting Statistics
Plotting Your Data
Command Line Options
The Complete Code
Running the Program
Summary
Experiments!
2: Spirographs
Parametric Equations
Spirograph Equations
Turtle Graphics
Requirements
The Code
The Spiro Constructor
The Setup Functions
The restart() Method
The draw() Method
Creating the Animation
The SpiroAnimator Class
The genRandomParams() Method
Restarting the Program
The update() Method
Showing or Hiding the Cursor
Saving the Curves
Parsing Command Line Arguments and Initialization
The Complete Code
Running the Spirograph Animation
Summary
Experiments!
Part II: Simulating Life
3: Conway's Game of Life
How It Works
Requirements
The Code
Representing the Grid
Initial Conditions
Boundary Conditions
Implementing the Rules
Sending Command Line Arguments to the Program
Initializing the Simulation
The Complete Code
Running the Game of Life Simulation
Summary
Experiments!
4: Generating Musical Overtones with the Karplus-Strong Algorithm
How It Works
The Simulation
Creating WAV Files
The Minor Pentatonic Scale
Requirements
The Code
Implementing the Ring Buffer with deque
Implementing the Karplus-Strong Algorithm
Writing a WAV File
Playing WAV Files with pygame
The main() Method
The Complete Code
Running the Plucked String Simulation
Summary
Experiments!
5: Boids: Simulating a Flock
How It Works
Requirements
The Code
Computing the Position and Velocities of the Boids
Setting Boundary Conditions
Drawing a Boid
Applying the Rules of the Boids
Adding a Boid
Scattering the Boids
Command Line Arguments
The Boids Class
The Complete Code
Running the Boids Simulation
Summary
Experiments!
Part III: Fun with Images
6: ASCII Art
How It Works
Requirements
The Code
Defining the Grayscale Levels and Grid
Computing the Average Brightness
Generating the ASCII Content from the Image
Command Line Options
Writing the ASCII Art Strings to a Text File
The Complete Code
Running the ASCII Art Generator
Summary
Experiments!
7: Photomosaics
How It Works
Splitting the Target Image
Averaging Color Values
Matching Images
Requirements
The Code
Reading in the Tile Images
Calculating the Average Color Value of the Input Images
Splitting the Target Image into a Grid
Finding the Best Match for a Tile
Creating an Image Grid
Creating the Photomosaic
Adding the Command Line Options
Controlling the Size of the Photomosaic
The Complete Code
Running the Photomosaic Generator
Summary
Experiments!
8: Autostereograms
How It Works
Perceiving Depth in an Autostereogram
Depth Maps
Requirements
The Code
Repeating a Given Tile
Creating a Tile from Random Circles
Creating Autostereograms
Command Line Options
The Complete Code
Running the Autostereogram Generator
Summary
Experiments!
Part IV: Enter 3D
9: Understanding OpenGL
Old-School OpenGL
Modern OpenGL: The 3D Graphics Pipeline
Geometric Primitives
3D Transformations
Shaders
Vertex Buffers
Texture Mapping
Displaying OpenGL
Requirements
The Code
Creating an OpenGL Window
Setting Callbacks
The Scene Class
The Complete Code
Running the OpenGL Application
Summary
Experiments!
10: Particle Systems
How It Works
Modeling the Motion of a Particle
Setting a Maximum Spread
Rendering the Particles
Using OpenGL Blending to Create More Realistic Sparks
Using Billboarding
Animating the Sparks
Requirements
The Code for the Particle System
Defining the Particle Geometry
Defining the Time-Lag Array for the Particles
Setting the Initial Particle Velocities
Creating the Vertex Shader
Creating the Fragment Shader
Rendering
The Camera Class
The Complete Particle System Code
The Box Code
The Code for the Main Program
Updating the Particles at Each Step
The Keyboard Handler
Managing the Main Program Loop
The Complete Main Program Code
Running the Program
Summary
Experiments!
11: Volume Rendering
How It Works
Data Format
Generating Rays
Displaying the OpenGL Window
Requirements
An Overview of the Project Code
Generating a 3D Texture
The Complete 3D Texture Code
Generating Rays
Defining the Color Cube Geometry
Creating the Frame Buffer Object
Rendering the Back-Faces of the Cube
Rendering the Front-Faces of the Cube
Rendering the Whole Cube
The Resize Handler
The Complete Ray Generation Code
Volume Ray Casting
The Vertex Shader
The Fragment Shader
The Complete Volume Ray Casting Code
2D Slicing
The Vertex Shader
The Fragment Shader
A User Interface for 2D Slicing
The Complete 2D Slicing Code
Putting the Code Together
The Complete Main File Code
Running the Program
Summary
Experiments!
Part V: Hardware Hacking
12: Introduction to the Arduino
The Arduino
The Arduino Ecosystem
Language
IDE
Community
Peripherals
Requirements
Building the Light-Sensing Circuit
How the Circuit Works
The Arduino Sketch
Creating the Real-Time Graph
The Python Code
The Complete Python Code
Running the Program
Summary
Experiments!
13: Laser Audio Display
Generating Patterns with a Laser
Motor Control
The Fast Fourier Transform
Requirements
Constructing the Laser Display
Wiring the Motor Driver
The Arduino Sketch
Configuring the Arduino's Digital Output Pins
The Main Loop
Stopping the Motors
The Python Code
Selecting the Audio Device
Reading Data from the Input Device
Computing the FFT of the Data Stream
Extracting Frequency Information from the FFT Values
Converting Frequency to Motor Speed and Direction
Testing the Motor Setup
Command Line Options
Manual Testing
The Complete Python Code
Running the Program
Summary
Experiments!
14: A Raspberry Pi–Based Weather Monitor
The Hardware
The DHT11 Temperature and Humidity Sensor
The Raspberry Pi
Setting Up Your Pi
Installing and Configuring Software
The Operating System
Initial Configuration
Wi-Fi Setup
Setting Up the Programming Environment
Connecting via SSH
The Bottle Web Framework
Plotting with flot
Shutting Down the Pi
Building the Hardware
The Code
Handling Sensor Data Requests
Plotting the Data
The update() Method
The JavaScript Handler for the LED
Adding Interactivity
The Complete Code
Running the Program
Summary
Experiments!
Appendix A: Software Installation
Installing Source Code for the Book's Projects
Installing on Windows
Installing GLFW
Installing Prebuilt Binaries for Each Module
Other Options
Installing on OS X
Installing Xcode and MacPorts
Installing Modules
Installing on Linux
Appendix B: Basic Practical Electronics
Common Components
Essential Tools
Building Circuits
Going Further
Appendix C: Raspberry Pi Tips & Tricks
Setting Up Wi-Fi
Checking Whether Your Pi Is Connected
Preventing the Wi-Fi Adapter from Going to Sleep
Backing Up Your Code and Data from the Pi
Backing Up Your Entire Pi OS
Logging In to Your Pi with SSH
Using the Raspberry Pi Camera
Enabling Sound on Your Pi
Making Your Pi Talk
Making HDMI Work
Making Your Pi Mobile
Checking Your Raspberry Pi Hardware Version
Index
Resources
More No-nonsense Books from No Starch Press
About the Author
P U T T H E F U N P U T T H E F U N B A C K I N B A C K I N P R O G R A M M I N G P R O G R A M M I N G Python is a powerful programming language that’s easy to learn and fun to play with. But once you’ve gotten a handle on the basics, what do you do next? • Make realistic animations with OpenGL shaders by exploring particle systems, transparency, and billboarding techniques Python Playground is a collection of imaginative programming projects that will inspire you to use Python to make art and music, build simulations of real-world phenomena, and interact with hardware like the Arduino and Raspberry Pi. You’ll learn to use common Python tools and libraries like numpy, matplotlib, and pygame to do things like: • Construct 3D visualizations using data from CT and MRI scans • Build a laser show that responds to music by hooking up your computer to an Arduino Programming shouldn’t be a chore. Have some solid, geeky fun with Python Playground. • Generate Spirograph-like patterns using parametric A B O U T T H E A U T H O R equations and the turtle module • Create music on your computer by simulating frequency overtones • Translate graphical images into ASCII art • Write an autostereogram program that produces 3D images hidden beneath random patterns Mahesh Venkitachalam is a software engineer with two decades of programming experience. He has nurtured a passion for technology since the eighth grade, which he channels into his popular electronics and programming blog, electronut.in. The projects in this book are compatible with Python 2 and 3. TH E FI N EST I N G E E K E NTE RTAI N M E NT ™ www.nostarch.com “I LI E FLAT.” This book uses a durable binding that won’t snap shut. $29.95 ($34.95 CDN) S H E L V E I N : P Y T H O N P R O G R A M M N G I L A N G U A G E S / PY T HON PY T HON PL AYGROUND PL AYGROUND G E E K Y P R O J E C T S F O R T H E C U R I O U S P R O G R A M M E R M A H E S H V E N K I T A C H A L A M P P Y Y T T H H O O N N P P L L A A Y Y G G R R O O U U N N D D V E N K I T A C H A L A M
Python Playground
Python Playground geeky Projects for the Curious Programmer by Mahesh Venkitachalam San Francisco
Python Playground. Copyright © 2016 by Mahesh Venkitachalam. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. First printing 19 18 17 16 15 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-604-4 ISBN-13: 978-1-59327-604-1 Publisher: William Pollock Production Editor: Serena Yang Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editor: William Pollock Technical Reviewers: Nicholas Kramer and Raviprakash Jayaraman Copyeditor: Kim Wimpsett Compositor: Kathleen Miller Proofreader: Paula L. Fleming Figure 8-4 was created by Fred Hsu (March 2005) and has been reproduced under the Creative Commons Attribution-Share Alike 3.0 Unported license. For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; info@nostarch.com www.nostarch.com Library of Congress Cataloging-in-Publication Data Venkitachalam, Mahesh. Python playground : geeky projects for the curious programmer / by Mahesh Venkitachalam. pages cm Includes index. ISBN 978-1-59327-604-1 -- ISBN 1-59327-604-4 1. Python (Computer program language) 2. Electronic apparatus and appliances--Automatic control. 3. Arduino (Programmable controller)--Programming. 4. Raspberry Pi (Computer)--Programming. I. Title. QA76.73.P98.V46 2015 005.13'3--dc23 2014046103 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
For my parents, A.V. Venkitachalam and N. Saraswathy, for giving me the greatest gift of all— an education & For Hema H = M 2A
B r i e f C o n t e n t s Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Part I: WarMIng UP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1: Parsing iTunes Playlists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Spirographs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Part II: SIMUlatIng lIfe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 3: Conway’s Game of Life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Chapter 4: Generating Musical Overtones with the Karplus-Strong Algorithm . . . . . . . . . . . 55 Chapter 5: Boids: Simulating a Flock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Part III: fUn WIth IMageS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Chapter 6: ASCII Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Chapter 7: Photomosaics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Chapter 8: Autostereograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Part IV: enter 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Chapter 9: Understanding OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Chapter 10: Particle Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Chapter 11: Volume Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Part V: harDWare hackIng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Chapter 12: Introduction to the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Chapter 13: Laser Audio Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Chapter 14: A Raspberry Pi–Based Weather Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Appendix A: Software Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Appendix B: Basic Practical Electronics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Appendix C: Raspberry Pi Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
分享到:
收藏