Image Processing in Python
Image Processing in Python
Processing raster images with the Pillow library
About the Book
Pillow is the most popular Python image processing library. It provides many of the features found in imaging applications like Photoshop or GIMP, such as loading, saving, resizing, transforming images, as well as converting colours and applying filters, enhancements, and effects. This book will teach you how to use simple Python code to automate these functions using the Pillow library, so that your programs can incorporate advanced image processing. It provides a comprehensive guide to Pillow, in plain language and with lots of examples.
Table of Contents
-
-
Preface
- Who is this book for?
- About the author
- Keep in touch
-
Introduction
- Versions
- Example sources on github
-
Preface
-
I Bitmap images
-
1 Introduction to bitmap imaging
- 1.1 What is a bitmap image?
- 1.2 Spatial sampling
- 1.3 Colour representation
- 1.4 File formats
- 1.5 Vector images
-
2 Computer colour
-
2.1 Visible light
- 2.1.1 Frequency and wavelength
-
2.2 What is colour?
- 2.2.1 Non-spectral colours
- 2.3 How we see colour
-
2.4 The RGB colour model
- 2.4.1 Displaying colour
- 2.4.2 Representing RGB colours as a percentage
- 2.4.3 Floating point representation
- 2.4.4 Byte value representation
- 2.5 Colour resolution
- 2.6 Greyscale colour model
-
2.7 The CMYK colour model
- 2.7.1 The K component
- 2.8 HSL/HSB colour models
- 2.9 HSL variants
-
2.10 Perceptual colour models
- 2.10.1 CIE spaces
-
2.11 Colour management
- 2.11.1 Gamuts
-
2.1 Visible light
-
3 Bitmap image data
- 3.1 Data layout
-
3.2 8-bit per channel images
- 3.2.1 24-bit RGB
- 3.2.2 32-bit CMYK
- 3.2.3 8-bit greyscale
- 3.2.4 32-bit RGBA
-
3.3 Bitmap data with fewer levels
- 3.3.1 8-bit RGB
- 3.3.2 16-bit RGB
- 3.3.3 Dithering
- 3.4 Bilevel images
- 3.5 Bitmap data with more levels
-
3.6 Palette based images
- 3.6.1 Images with more than 256 colours
-
3.7 Handling transparency
- 3.7.1 Alpha channel
- 3.7.2 Transparent palette entry
- 3.7.3 Transparent colour
- 3.8 Interlacing and alternate pixel ordering
-
4 Image file formats
- 4.1 Why are there so many formats?
- 4.2 Image data and metadata
-
4.3 Image compression
- 4.3.1 Lossless compression
- 4.3.2 Lossy compression
-
4.4 Some common file formats
- 4.4.1 PNG format
- 4.4.2 JPEG format
- 4.4.3 GIF format
- 4.4.4 BMP format
- 4.5 Animation
-
1 Introduction to bitmap imaging
-
II Pillow library
-
5 Introduction to Pillow
- 5.1 Pillow and PIL
- 5.2 Installing Pillow
- 5.3 Main features of Pillow
-
6 Basic imaging
- 6.1 The Image class
- 6.2 Creating and displaying an image
- 6.3 Saving an image
-
6.4 Handling colours
- 6.4.1 Converting strings to colours
- 6.5 Creating images
- 6.6 Opening an image
- 6.7 Image processing
- 6.8 Rotating an image
- 6.9 Creating a thumbnail
- 6.10 Image modes
-
7 Image class
- 7.1 Example code
-
7.2 Creating images
- 7.2.1 Image.new
- 7.2.2 Image.open
- 7.2.3 copy
- 7.2.4 Other methods
- 7.3 Saving images
- 7.4 Image generators
-
7.5 Working with image bands
- 7.5.1 getbands
- 7.5.2 split
- 7.5.3 merge
- 7.5.4 getchannel
- 7.5.5 putalpha
-
8 ImageOps module
-
8.1 Image resizing functions
- 8.1.1 expand
- 8.1.2 crop
- 8.1.3 scale
- 8.1.4 pad
- 8.1.5 fit
-
8.2 Image transformation functions
- 8.2.1 flip
- 8.2.2 mirror
- 8.2.3 exif-transpose
-
8.3 Colour effects
- 8.3.1 grayscale
- 8.3.2 colorize
- 8.3.3 invert
- 8.3.4 posterize
- 8.3.5 solarize
-
8.4 Image adjustment
- 8.4.1 autocontrast
- 8.4.2 equalize
-
8.5 Deforming images
- 8.5.1 How deform works
- 8.5.2 getmesh
- 8.5.3 A wave transform
- 8.5.4 Other deformations
-
8.1 Image resizing functions
-
9 Image attributes and statistics
-
9.1 Attributes
- 9.1.1 File size
- 9.1.2 File name
- 9.1.3 File format
- 9.1.4 Mode and bands
- 9.1.5 Palette
- 9.1.6 Info
- 9.1.7 Animation
- 9.1.8 EXIF tags
-
9.2 Image statistics
- 9.2.1 Image histogram
- 9.2.2 Masking
- 9.2.3 Other Image statistics
- 9.2.4 ImageStat module
-
9.1 Attributes
-
10 Enhancing and filtering images
-
10.1 ImageEnhance
- 10.1.1 Brightness
- 10.1.2 Contrast
- 10.1.3 Color
- 10.1.4 Sharpness
- 10.2 ImageFilter
- 10.3 Predefined filters
-
10.4 Parameterised filters
- 10.4.1 Blurring functions
- 10.4.2 Unsharp masking
- 10.4.3 Ranking and averaging filters
- 10.5 Defining your own filters
-
10.1 ImageEnhance
-
11 Image compositing
-
11.1 Simple blending
- 11.1.1 Image transparency
- 11.1.2 ImageChops blend function
- 11.1.3 ImageChops composite function
-
11.2 Blend modes
- 11.2.1 Addition
- 11.2.2 Subtraction
- 11.2.3 Lighter and darker
- 11.2.4 Multiply and screen
- 11.2.5 Other blend modes
- 11.3 Logical combinations
-
11.1 Simple blending
-
12 Drawing on images
- 12.1 Coordinate system
-
12.2 Drawing shapes
- 12.2.1 Drawing rectangles
- 12.2.2 Drawing other shapes
- 12.2.3 Points
-
12.3 Handling text
- 12.3.1 Drawing simple text
- 12.3.2 Font and text metrics
- 12.3.3 Anchoring
- 12.3.4 Drawing multiline text
-
12.4 Paths
- 12.4.1 Drawing a path
- 12.4.2 Transforming paths
- 12.4.3 Mapping points
-
13 Accessing pixel data
- 13.1 Processing an image
- 13.2 Creating an image
- 13.3 Performance
-
14 Integrating Pillow with other libraries
-
14.1 NumPy integration
- 14.1.1 Converting a Pillow image to Numpy
- 14.1.2 Image data in a NumPy array
- 14.1.3 Modifying the NumPy image
- 14.1.4 Converting a NumPy array to a Pillow image
-
14.1 NumPy integration
-
5 Introduction to Pillow
-
III Reference
-
15 Pillow colour representation
- 15.1 Hexadecimal colour specifiers
- 15.2 RGB functions
- 15.3 HSL functions
- 15.4 HSV functions
- 15.5 Named colours
- 15.6 Example
- 15.7 Image modes
-
More books from this author
- Numpy Recipes
- Computer Graphics in Python with Pycairo
- Functional Programming in Python
-
15 Pillow colour representation
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,817,855writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
Stratospheric
Tom Hombergs, Björn Wilmsmann, and Philip RiecksFrom Zero to Production with Spring Boot and AWS. All you need to know to get a Spring Boot application into production with AWS. No previous AWS knowledge required.
Go to stratospheric.dev for a tour of the contents.
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
node-opcua by example
Etienne RossignonGet the best out of node-opcua through a set of documented examples by the author himself that will allow you to create stunning OPCUA Servers or Clients.
Advanced Web Application Architecture
Matthias NobackThe missing manual for making your web applications future-proof
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Thinking with Types
Sandy MaguireThis book aims to be the comprehensive manual for type-level programming. It's about getting you from here to there---from a competent Haskell programmer to one who convinces the compiler to do their work for them.
Maîtriser Apache JMeter
Philippe Mouawad, Bruno Demion (Milamber), and Antonio Gomes RodriguesToute la puissance d'Apache JMeter expliquée par ses commiteurs et utilisateurs experts. De l'intégration continue en passant par le Cloud, vous découvrirez comment intégrer JMeter à vos processus "Agile" et Devops.
If you're looking for the newer english version of this book, go to Master JMeter : From load testing to DevOps
Top Bundles
- #1
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #4
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #5
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #10
Cloud Architect: Transform Technology and Organization
2 Books
Architects don't just recite product names and features. They understand the options, decisions, and trade-offs behind them. They earn credibility and maintain authenticity by connecting the penthouse with the engine room. Get two essential books that redefine the role of the software and IT architect at one low price:37 Things One Architect...