This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
About the Technical Reviewers
Ethan Furman
Ethan, a largely self-taught programmer, discovered Python around the turn of the century, but wasnt able to explore it for nearly a decade. When he finally did, he fell in love with its simple syntax, lack of boiler-plate, and the ease with which one can express ones ideas in code. After writing a dbf library to aid in switching his companys code over to Python, he authored PEP 409, wrote the Enum implementation for PEP 435, and authored PEP 461. He was invited to be a core developer after PEP 435, which he happily accepted.
He thanks his mother for his love of language, stories, and the written word.
Alessia Marcolini
Alessia is a Data Science EIT Digital student at the Eindhoven University of Technology. She is a Junior Data Scientist at HK3lab, working on machine learning/deep learning frameworks to integrate multiple medical imaging modalities and different clinical data to get more precise prognostic/diagnostic biomarkers for human and veterinary health.
She has been a volunteer of the Italian Python Community since 2017, helping with the organization of PyCon Italy (the national Python Conference, hosting 700+ international delegates each year). In 2018, she also joined the organization committee of EuroSciPy, the European Conference for Python in Science.
When not coding, she loves dancing and drinking black tea and good gin.
Acknowledgments
Writing a book takes a huge time commitment. Fortunately, I had a couple of very helpful people right out the gate. Ethan Furman joined me for this book once more as a technical reviewer and editor. I also had Alessia Marcolini helping out as a technical reviewer and giving me lots of feedback. Thank you so much!
Mike Barnett (creator of PySimpleGUI) encouraged me to write a book on Pillow and has been helpful in feedback on the GUI examples in the book. Steve Barnes has also helped out with finding little bugs or making good suggestions that have helped make the chapters better.
Id also like to thank my beta readers:
- Jase Lindgren, an early beta reader, who helped me find some typos and provided good feedback too.
- Ruud van der Ham also helped with some good comments about PySimpleGUI and Pillow as well.
- Roy Neilsen who reviewed many of the chapters in the book
Thank you all for your help!
Alex Clark, the creator of the Pillow fork, has also been encouraging. Thank you to everyone who has been and who is currently a core developer of Pillow. You are amazing!
And to anyone that I am forgetting and to you, thank you too!
Thank you for reading this book!
Introduction
The Python programming language has thousands of packages that you can install from the Python Package Index that you can use to enhance your code. There are many popular packages for working with images in Python, such as scikit-image, OpenCV and NumPy. However, for this book, you will be learning how to use the Pillow package, which is the friendly fork of the Python Imaging Library.
The Python Imaging Library adds image processing capabilities to your Python installation. Pillow is the Python 3 version of the Python Imaging Library. According to Pillows documentation, it provides you with extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.
By the end of this book, you will be able to do all of the following tasks:
- Open images
- Extract histrogram data
- Extract image metadata
- Apply image transforms
- Add filters
- Crop images
- Enhance images
- Combine images
- Basic drawings
- and more!
Pillow is a powerful library that you can use for general purpose image processing. If you need to do pixel-by-pixel operations where you need to examine or change pixels, you will find that Pillow is relatively slow. You may want to look at a library like NumPy instead for that use case.
You will also be using the PySimpleGUI toolkit to build little applications in combination with Pillow. The PySimpleGUI portions of the books are completely optional, but are helpful in demonstrating the concepts you will be learning about. You can skip them entirely if you want to.
Who is this book for?
This book is targeted at intermediate level developers. The ideal person reading this book will know the Python language already and understand how to install 3rd party packages.
About the Author
Mike Driscoll has been programming with the Python language for more than a decade. When Mike isnt programming for work, he writes about Python on his blog and contributes to Real Python. He has worked with Packt Publishing and No Starch Press as a technical reviewer. Mike has also written several books.
You can see a full listing of Mikes books on his blog too.
Conventions
There arent a lot of special conventions in this book. The main one you need to be aware of is that code blocks will look like this:
1
import
PIL
2
3
# do something fun!
When referring to code in a sentence you will see it in monospace
.
Images will be marked with a chapter and image number for easy reference.
Other than that, there are no conventions!
Requirements
This book is written for Python 3. The examples may work in Python 2, but are not tested in Python 2.
You will need Pillow to be able to use the examples in this book. Installing Pillow can be done using pip
:
1
python3 -m pip install --upgrade pip2
python3 -m pip install --upgrade Pillow
Linux users can also install Pillow by using their Linux package manager and installing python-imaging
if they prefer.
If you want to install Pillow from source, you should refer to the documentation for the latest instructions.
If you want to be able to use the PySimpleGUI examples in this book, then you will need to install it as well using the following command:
1
python3 -m pip install PySimpleGUI
Note: PySimpleGUI is licensed with the LGPLv3 license, which requires you to keep the library open source. If you plan to use PySimpleGUI, make sure you understand the restrictions that LGPLv3 has in regards to commercial use.
PySimpleGUI uses Tkinter by default. You can use PySimpleGUI with wxPython or PyQt simply by changing the import
. For this book, you will use the default PySimpleGUI.
On Ubuntu, you may need to install python3-tk
as PySimpleGUI depends on Tkinter which isnt always included in the system Python installation.