Python Multimedia Beginner's Guide
Total Page:16
File Type:pdf, Size:1020Kb
Python Multimedia Beginner's Guide Learn how to develop multimedia applications using Python with this practical step-by-step guide Ninad Sathaye BIRMINGHAM - MUMBAI x x Python Multimedia Beginner's Guide Copyright © 2010 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2010 Production Reference: 1060810 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849510-16-5 www.packtpub.com Cover Image by Ed Maclean ([email protected]) x x Credits Author Editorial Team Leader Ninad Sathaye Aanchal Kumar Reviewers Project Team Leader Maurice HT Ling Priya Mukherji Daniel Waterworth Sivan Greenberg Project Coordinator Prasad Rai Acquisition Editor Steven Wilding Proofreader Lynda Sliwoski Development Editor Eleanor Duffy Graphics Geetanjali Sawant Technical Editor Charumathi Sankaran Production Coordinators Shantanu Zagade Indexers Aparna Bhagat Hemangini Bari Tejal Daruwale Cover Work Aparna Bhagat x x About the Author Ninad Sathaye ([email protected]) has more than six years of experience in software design and development. He is currently working at IBM, India. Prior to working for IBM, he was a Systems Programmer at Nanorex Inc. based in Michigan, U.S.A. At Nanorex, he was involved in the development of an open source, interactive 3D CAD software, written in Python and C. This is where he developed passion for the Python programming language. Besides programming, his favorite hobbies are reading and traveling. Ninad holds a Master of Science degree in Mechanical Engineering from Kansas State University, U.S.A. I would like to thank everyone at Packt Publishing, especially, Eleanor Duffy, Steven Wilding, Charu Sankaran, and Prasad Rai for their co-operation. This book wouldn't have been possible without your help. I also want to thank all the technical reviewers of the book for their valuable suggestions. I wish to express my sincere thanks and appreciation to Rahul Nayak, my colleague, who provided many professional quality photographs for this book. I owe a special thanks to Mark Sims and Bruce Smith, my former colleagues, for introducing me to the amusing world of Python. Finally, this book wouldn't have been possible without the encouragement and support of my whole family. I owe my loving thanks to my wife, Arati, for providing valuable feedback. She also happens to be the photographer of several of the pictures used throughout this book. x x About the Reviewers Maurice HT Ling completed his Ph.D. in Bioinformatics and B.Sc (Hons) in Molecular and Cell Biology, where he worked on microarray analysis and text mining for protein-protein interactions. He is currently an Honorary Fellow at The University of Melbourne and a Lecturer at Singapore Polytechnic where he lectures on microbiology and computational biology. Maurice holds several Chief Editorships including The Python Papers, iConcept Journal of Computational and Mathematical Biology, and Methods and Cases in Computational, Mathematical, and Statistical Biology. In his free time, Maurice likes to train in the gym, read, and enjoy a good cup of coffee. He is also a Senior Fellow of the International Fitness Association, U.S.A. Daniel Waterworth is a Python fanatic who can often be found behind his keyboard. He is always beavering away on a new project having learned to program from a young age. He is a keen blogger and his ideas can be found at http://active-thought.com. Sivan Greenberg is a Forum Nokia Champion, with almost ten years of multi-disciplinary IT experience and a sharp eye for quality. He started with open source technologies and the Debian project back in 2002. Joining Ubuntu development two years later, Sivan also contributed to various other open source projects, such as Plone and Nokia's Maemo. He has experience with quality assurance, application and web development, UNIX system administration (including some rather exotic IBM platforms), and GUI programming and documentation. He's been using Python for all of his development needs for the last five years. He is currently involved with Nokia's MeeGo project and works with CouchDB and Python in his day job for a living. I thank my unique and amazing family, specifically my Dad Eric for igniting the spark of curiosity from day zero. x x x x To my daughter, Anvita x x x x Table of Contents Preface 1 Chapter 1: Python and Multimedia 7 Multimedia 8 Multimedia processing 8 Image processing 8 Audio and video processing 10 Compression 10 Mixing 11 Editing 11 Animations 11 Built-in multimedia support 12 winsound 12 audioop 12 wave 13 External multimedia libraries and frameworks 13 Python Imaging Library 13 PyMedia 13 GStreamer 13 Pyglet 14 PyGame 14 Sprite 14 Display 14 Surface 14 Draw 14 Event 15 Image 15 Music 15 Time for action – a simple application using PyGame 15 QT Phonon 18 Other multimedia libraries 19 x x Table of Contents Snack Sound Toolkit 19 PyAudiere 20 Summary 20 Chapter 2: Working with Images 21 Installation prerequisites 21 Python 21 Windows platform 22 Other platforms 22 Python Imaging Library (PIL) 22 Windows platform 22 Other platforms 22 PyQt4 23 Windows platform 23 Other platforms 24 Summary of installation prerequisites 24 Reading and writing images 25 Time for action – image file converter 25 Creating an image from scratch 28 Time for action – creating a new image containing some text 28 Reading images from archive 29 Time for action – reading images from archives 29 Basic image manipulations 30 Resizing 30 Time for action – resizing 30 Rotating 33 Time for action – rotating 34 Flipping 35 Time for action – flipping 35 Capturing screenshots 36 Time for action – capture screenshots at intervals 36 Cropping 39 Time for action – cropping an image 39 Pasting 40 Time for action – pasting: mirror the smiley face! 40 Project: Thumbnail Maker 42 Time for action – play with Thumbnail Maker application 43 Generating the UI code 45 Time for action – generating the UI code 45 Connecting the widgets 47 Time for action – connecting the widgets 48 Developing the image processing code 49 [ ii ] x x Table of Contents Time for action – developing image processing code 49 Summary 53 Chapter 3: Enhancing Images 55 Installation and download prerequisites 56 Adjusting brightness and contrast 56 Time for action – adjusting brightness and contrast 56 Tweaking colors 59 Time for action – swap colors within an image! 59 Changing individual image band 61 Time for action – change the color of a flower 61 Gray scale images 63 Cook up negatives 64 Blending 65 Time for action – blending two images 65 Creating transparent images 68 Time for action – create transparency 68 Making composites with image mask 70 Time for action – making composites with image mask 71 Project: Watermark Maker Tool 72 Time for action – Watermark Maker Tool 73 Applying image filters 81 Smoothing 82 Time for action – smoothing an image 82 Sharpening 84 Blurring 84 Edge detection and enhancements 85 Time for action – detecting and enhancing edges 85 Embossing 87 Time for action – embossing 87 Adding a border 88 Time for action – enclosing a picture in a photoframe 89 Summary 90 Chapter 4: Fun with Animations 91 Installation prerequisites 92 Pyglet 92 Windows platform 92 Other platforms 92 Summary of installation prerequisites 93 Testing the installation 93 A primer on Pyglet 94 [ iii ] x x Table of Contents Important components 94 Window 94 Image 95 Sprite 95 Animation 95 AnimationFrame 95 Clock 95 Displaying an image 96 Mouse and keyboard controls 97 Adding sound effects 97 Animations with Pyglet 97 Viewing an existing animation 97 Time for action – viewing an existing animation 98 Animation using a sequence of images 100 Time for action – animation using a sequence of images 100 Single image animation 102 Time for action – bouncing ball animation 102 Project: a simple bowling animation 108 Time for action – a simple bowling animation 108 Animations using different image regions 113 Time for action – raindrops animation 114 Project: drive on a rainy day! 117 Time for action – drive on a rainy day! 118 Summary 122 Chapter 5: Working with Audios 123 Installation prerequisites 123 GStreamer 124 Windows platform 124 Other platforms 125 PyGobject 125 Windows platform 125 Other platforms 125 Summary of installation prerequisites 126 Testing the installation 127 A primer on GStreamer 127 gst-inspect and gst-launch 128 Elements and pipeline 128 Plugins 129 Bins 129 Pads 130 Dynamic pads 130 Ghost pads 131 Caps 131 [ iv ] x x Table of Contents Bus 131 Playbin/Playbin2 131 Playing music 132 Time for action – playing an audio: method 1 133 Building a pipeline from elements 137 Time for action – playing an audio: method 2 138 Playing an audio from a website 141 Converting audio file format 142 Time for action – audio file format converter 142 Extracting part of an audio 150 The Gnonlin plugin 151 Time for action – MP3 cutter! 152 Recording 156 Time for action – recording 157 Summary 160 Chapter 6: Audio Controls and Effects 161 Controlling playback 161 Play 162 Pause/resume 162 Time for action – pause and resume a playing audio stream 162 Stop 165 Fast-forward/rewind 166 Project: extract audio using playback controls 166 Time for action – MP3 cutter from basic principles 167 Adjusting volume 173 Time for action – adjusting volume 173 Audio effects 175 Fading effects 175 Time for action – fading effects 176 Echo echo echo..