Image Processing in Python (Martin McBride) (Z-Library)

Author: Martin McBride

技术

No Description

📄 File Format: PDF
💾 File Size: 5.9 MB
27
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
(This page has no text content)
📄 Page 2
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 * * * * * 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
📄 Page 3
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
📄 Page 4
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
📄 Page 5
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
📄 Page 6
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
📄 Page 7
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
📄 Page 8
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.
📄 Page 9
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 (info@axlesoft.com).
📄 Page 10
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
📄 Page 11
I B
📄 Page 12
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
📄 Page 13
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.
📄 Page 14
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 modern systems use 8 bits per channel (for example, three integers between 0 and 255 to store the red, green, and blue values). This gives a precision of better than 0.5%, which is adequate for most uses. However, some image formats store data using fewer or more bits per colour, so it is useful to understand the different formats available. Finally, we need to be aware that a computer system cannot capture or display the full range of colours available in the real world. The most obvious aspect of that is colour intensity. In the real world, we might encounter the full glare of the sun or the total darkness of a deep cave. There is no possibility of replicating that range of intensities on a computer monitor, and even less possibility of recreating it on a computer printout. A printer can’t create anything brighter than the paper itself, and it can’t create anything darker than the black ink (which is probably dark grey at best). Leaving aside the intensity, many natural colours are too pure and vibrant to be accurately recreated by a screen or printer. We can only approximate them on a computer. 1.4 File formats
📄 Page 15
Bitmap images are often stored in a file, and there are many different types of file formats in use. There are three main aspects of an imaging file format that make it what it is: All bitmap images must store the bitmap data, of course. Different file types have different capabilities in terms of the colour models and bit depths they support. Image data is often very big, so many file formats support data compression. There are many schemes. Some are more efficient than others, and some are more suited to specific types of images. Finally, all file formats support metadata, see the note. Metadata means data about other data. It is extra information in an image file that describes the image data. This can vary from the most basic information (such as the image dimensions and colour type) right through to highly detailed information about the camera settings used when the image was taken. Each format has a different set of capabilities. These days the most popular formats are: JPEG format for photographic images, mainly because it supports a very efficient form of lossy compression that works well in photographs. PNG format for diagrams and logos, mainly because it supports a very efficient form of lossless compression that works well with artificial images. GIF format, which isn’t used much for normal images, but has a unique feature of supporting simple animations, that makes it useful for certain web pages. TIFF format, which is mainly used for professional printing applications. It is a very capable format that supports a huge range of data types, compressions schemes, and metadata, but it is also quite complex so it isn’t supported by browsers. There are many other file types, particularly for small files like icons, because they are small so they don’t require sophisticated compression. They are often specific to particular operating systems or types of software.
📄 Page 16
1.5 Vector images An alternative way of storing image data is to use a vector format. In a vector image, the image isn’t stored as pixels. Instead, it is stored as a set of mathematical definitions of shapes. A vector image is essentially a list of lines, rectangles, circles and other shapes. It defines the exact size, position, and colour of each shape, often as human-readable text. To view a vector image it must first be rendered, that is it is converted to a bitmap by, essentially, drawing each shape. This process is usually highly optimised. Advantages of vector images are: The file size is often much smaller than the equivalent bitmap image. The image can be rendered at any resolution because the exact positions of every shape are stored. You can zoom in on the image almost infinitely and it will still have perfect edges. Individual shapes in the image can be edited. Disadvantages are: The image must be rendered before it is displayed, which requires more processing power than a simple bitmap. There can be compatibility problems. Since the format is typically more complex, there are more ways for things to go wrong. It only really works for artificial images (diagrams, text documents etc), you can’t efficiently store a natural photograph in a vector format. Well-known vector formats are SVG, PDF and PostScript. We don’t cover vector formats in this book, it is a whole separate topic, but it is covered in my book Computer Graphics in Python.
📄 Page 17
2 Computer colour In this chapter we will take an in-depth look at how computers represent colour: Visible light - what is colour? How we see colour. The RGB colour model. Colour resolution. Greyscale colour model. Transparency The CMYK colour model. HSL/HSB colour models. Perceptual colour models. Colour management. 2.1 Visible light Visible light - the light our eyes can see - is a form of electromagnetic radiation. Electromagnetic radiation refers to waves in the electromagnetic field that radiated through space, carrying energy. Radio waves, microwaves, infra-red, visible light, ultraviolet, x-rays, and gamma rays are all different types of electromagnetic radiation. They have different names because some of them were first discovered and investigated by scientists before anyone realised they were linked. Despite these phenomena appearing to be very different, they are in fact all exactly the same thing - oscillations in the electromagnetic field that travel through space at the speed of light. The difference is the oscillation frequency. For example, analogue radio waves used by public broadcasters have frequencies of between 300 kHz (300 thousand oscillations per second) for medium wave AM, through to 300 MHz (300 million oscillations per second) for FM radio. On an analogue radio, you will tune to a particular frequency to listen to a particular station (eg 95 FM means 95 MHz).
📄 Page 18
2.1.1 Frequency and wavelength All electromagnetic radiation, including light, travels at a speed of 299 792 458 metres per second. That is the speed of light in a vacuum, often called c (light travels very slightly slower if it passes through materials such as air, glass or water, but the difference is a tiny fraction of 1%). For a particular frequency f, electromagnetic radiation has a wavelength λ given by: λ = c / f where c is speed of light For example a 100 MHz radio signal has a wavelength of 3 metres. This diagram shows some well-known types of electromagnetic radiation, listed in order of decreasing wavelength: Visible light occupies a very small range of the spectrum, between infrared and ultraviolet, with wavelengths between about 400nm and 700nm. nm stands for nanometre, and 1nm is equal to a millionth of a millimetre. 2.2 What is colour? We can see electromagnetic radiation in the range 400 nm to 700 nm, but why do we see different colours? A simple explanation is that different wavelengths appear as different colours. Here is an illustration:
📄 Page 19
Light with a wavelength of around 700 nm looks red, light with a wavelength of about 610 nm looks orange, and so on. You may recognise these as being the seven colours of the rainbow. The last three colours of the rainbow are classically called blue, indigo, violet. If you look at an actual spectrum it is more accurate to describe the last three bands as cyan, blue, violet. It is possible that when Newton first described the colours, what he meant by blue was something closer to what we now know as cyan (a light sky blue). But there are far, far more than 7 colours in the spectrum. Here is an illustration of the spectrum between red and orange: As you can see there is a whole range of subtly different colours present, that mix different amounts of red and orange. The same is true for each of the other adjacent colours. In fact, there is an almost infinite range of different colours in the visible spectrum, although some of them are so similar that the human eye can’t even tell them apart.
📄 Page 20
These are known as spectral colours. They are colours that correspond to light of a single wavelength, and they are also the colours that appear in a rainbow. 2.2.1 Non-spectral colours Most of the light we see doesn’t contain just a single wavelength of light. That is because most light sources (such as sunlight or many forms of artificial light) contain a mixture of many different wavelengths. When that light bounces off a surface, that surface will absorb or reflect different wavelengths to differing degrees. This means that most of the light the enters our eyes contains a mixture of different wavelengths. But we perceive it as being a single colour. Here are some colours that don’t exist on the spectrum: There is no single wavelength that looks hot pink or white. Those are colours that we can only see if a certain combination of different wavelengths is present 2.3 How we see colour If you think about the infinite number of colours in the spectrum and then think about the number of ways you could mix every combination of those colours in different amounts, it might seem like an impossible task to replicate that on a computer screen.
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List