Explore 1.5M+ audiobooks & ebooks free for days

Only $12.99 CAD/month after trial. Cancel anytime.

Image Processing in Python
Image Processing in Python
Image Processing in Python
Ebook357 pages2 hours

Image Processing in Python

Rating: 0 out of 5 stars

()

Read preview

About this ebook

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.

LanguageEnglish
PublisherMartin McBride
Release dateApr 1, 2022
ISBN9798201150280
Image Processing in Python

Read more from Martin Mc Bride

Related to Image Processing in Python

Related ebooks

Computers For You

View More

Reviews for Image Processing in Python

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Image Processing in Python - Martin McBride

    Image Processing in Python

    Image Processing in Python

    Processing raster images with the Pillow library

    Martin McBride

    This book is for sale at http://leanpub.com/imageprocessinginpython

    This version was published on 2021-08-22

    publisher's logo

    *   *   *   *   *

    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.

    *   *   *   *   *

    © 2021 Martin McBride

    Table of Contents

    Preface

    Who is this book for?

    About the author

    Keep in touch

    Introduction

    Versions

    Example sources on github

    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

    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

    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

    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

    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

    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

    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

    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

    Preface

    This book provides an introduction to the basics of image processing in Python, using the Pillow imaging library. After reading this book you should be able to create Python programs to read, write and manipulate images.

    Who is this book for?

    This book is aimed at anyone wishing to learn about image processing in Python. It doesn’t require any prior knowledge of image processing, but it will also be useful if you already have experience working with image data and would like to learn the specifics of the Pillow library.

    It will be assumed that you have a basic working knowledge of Python, but all examples are fully explained and don’t use any advanced language features.

    About the author

    Martin McBride is a software developer, specialising in computer graphics, sound, and mathematical programming. He has been writing code since the 1980s in a wide variety of languages from assembler through to C++, Java and Python. He writes for PythonInformer.com and is the author of several books on Python. He is interested in generative art and works on the generativepy open source project.

    Keep in touch

    If you have any comments or questions you can get in touch by any of the following methods:

    Joining the Python Informer forum at http://pythoninformer.boards.net/.

    Signing up for the Python Informer newsletter at pythoninformer.com

    Following @pythoninformer on Twitter.

    Contacting me directly by email ([email protected]).

    Introduction

    This book is about bitmap imaging in Python. It is divided into two sections:

    Bitmap images - introduces some important concepts of bitmap imaging, including colour representation, pixel data models, image compression, file formats and metadata.

    Pillow library - a detailed tutorial on the Python Pillow imaging library, one of the most popular Python imaging libraries.

    There is also a Reference section, containing some useful information that you will probably need to refer too, all gathered in one place.

    Versions

    This book uses Pillow version 8.2.0 and Python version 3.9.

    The examples will work with older versions (Pillow version 5 or later, Python version 3.6 or later).

    There is a good chance the examples will also work with newer versions.

    Example sources on github

    You can find example images and source files on github, at https://github.com/martinmcbride/python-imaging-book-examples

    I Bitmap images

    1 Introduction to bitmap imaging

    This part of the book covers bitmap imaging.

    This chapter will cover the basics of what a bitmap image is. Later chapters will cover:

    How computers represent colour.

    Colour models.

    Colour resolution.

    How image data is stored in memory.

    Transparency.

    Image compression.

    Image file formats.

    Colour management.

    1.1 What is a bitmap image?

    You most likely already know what a bitmap image is. Almost any image you see on the web will be a bitmap image, and you have probably used your smartphone or digital camera to capture photographs as bitmap images.

    You might be more familiar with alternative names - raster image, or pixel image. They mean the same thing as bitmap image. They are sometimes also called JPEG images or PNG images, named after specific image file formats.

    You probably also know that a bitmap is made up of pixels - they can be thought of as tiny coloured squares that make up the image. They are normally too small to see but become visible if you zoom in too far and the image becomes pixelated.

    This chapter presents an overview of the characteristics of bitmap images, in preparation for the remaining part of this section that looks at bitmap images in detail.

    1.2 Spatial sampling

    A real-world scene has an almost infinite amount of detail. If you look out at, for example, a boat by a lakeside, it goes beyond the detail your eye can see. You could walk up to the boat and look at it in virtually unlimited detail.

    A bitmap image has a finite amount of detail. If you took a digital photograph of the boat, the camera would convert it into an array of pixels. Each pixel represents a small part of the image.

    If the pixels are very small together, we can’t distinguish the individual, and the image looks similar to the actual scene. If they are larger, we see the image as a set of pixels rather than a natural image, as this illustration shows:

    In practical terms, at a viewing distance of 40 cm, the eye can resolve objects that are about 0.1 mm apart. So for example, imagine a sheet of paper with two thin lines drawn 0.1 mm apart. If you held the paper 40 cm in from of your face, assuming you have normal eyesight, you might just about be able to see that there were two separate lines. Any further away and they would just look like a single line.

    This means that if you wanted to print an image on a page so that the eye couldn’t see the individual pixels, you should aim for a pixel resolution of 10 pixels per mm (about 250 pixels per inch) or better. So for a printed photograph or 15 cm by 10 cm, you would want an image of at least 1500 by 1000 pixels. That would be a 1.5 megapixel (MP) image.

    In reality, you would probably want a higher resolution than that:

    To allow you to print larger photographs.

    To allow you to crop a photograph to show just the subject.

    Most modern digital cameras support image sizes of 6 MP or higher.

    However, very large pixel sizes are not always as useful as they might seem:

    For printing very large images, such as posters, they are normally viewed from further back than 40 cm, so there is no need to have 0.1 mm spatial resolution.

    At very high resolutions, factors such as camera shake will blur the image, so there is little point in taking a very high-resolution photograph without a very solid tripod.

    1.3 Colour representation

    Each pixel in a bitmap image has a specific colour. There are various ways we might represent a colour in an image.

    The most common way to represent a colour is as three separate components, the amount of red, green, and blue light that make up the colour. As we will see, this is based on the way the human eyes perceives colour. However, we sometimes use alternate methods, including:

    CMYK - used to represent colours for printing.

    HSL - used in art and design as an intuitive way to select related colours.

    Perceptual colour spaces such as CieLAB used to create very accurate colours.

    An important consideration is how precisely we need to represent each colour in an image. As rough a rule of thumb, we can detect variations in colour of about 1%. Most

    Enjoying the preview?
    Page 1 of 1