Pillow is a free and open source library for the Python programming language that allows you to easily create & manipulate digital images. In this tutorial, you will
95 pages

431 KB – 95 Pages

PAGE – 2 ============
Python Pillow ii python for image manipulation. Pillow is a free and open source library for the Python programming language that allows you to easily create & manipulate digita l images. In this tutorial, you wi ll see the hands – on approach to learn different functionalities of pillow, much more than, r ead & save an image, creat ing thumbnail & merge to images, blur, crop, flip & rotate images, r esizing & adding watermark , a dding fi lters & working with colors to an image and u se of pillow & numpy in machine learning. This tutorial is basically designed to work as a guide for d evelopers who wan ts to learn python capabilities, automate image editing . It is also for the begin ners who w ish to know the image processing capabilities of python using pillow pa ckage and for the w eb developers who wants t o update and use images with logo s & watermark on their websites. It would be helpful if you have prior knowledge on a ny of the below mentioned technologies such as a ccess to computer & python is installed in it (else we need to install it), b asic understanding of python data types and functions and a bility to install Python . Copyright 20 20 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e – book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e – book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e – book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain i naccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please noti fy us at [email protected]

PAGE – 3 ============
Python Pillow iii About the tutorial .. .. .. .. ii Audience .. .. .. .. .. ii Prerequisites .. .. .. .. .. ii Copyright & Disclaimer .. .. .. .. . ii Table of Contents .. .. .. .. .. iii 1. .. .. .. .. 1 2. Python Pillow Environment Setup .. .. .. .. 2 Installing Pillow using pip .. .. .. .. 2 3. Python Pillow Using Imag e Module .. .. .. 3 Opening, rotating and displaying an image .. .. .. .. 3 Attributes of Image Module .. .. .. . 5 4. Python Pillow Working with Images .. .. .. .. 8 Reading an Image .. .. .. .. 8 Saving an Image .. .. .. .. . 10 5. Python Pillow Creating Thumbnails .. .. .. . 12 6. Python Pillow Merging Images .. .. .. .. 15 7. Python Pillow Blur an Image .. .. .. 22 Simple blur .. .. .. .. 22 Box blur .. .. .. .. . 24 Gaussian Blur .. .. .. .. .. 26 8. Python Pillow Cropping an Image .. .. .. . 29 9. Python Pillow Flip and Rotate Images .. .. .. . 32 10. Python Pillow Resizing an Image .. .. .. 39 Resize and save the resized image .. .. .. . 39 11. Python Pillow Cre ating a Watermark .. .. .. .. 42 12. Python Pillow Adding Filters to an Image .. .. 4 5

PAGE – 4 ============
Python Pillow iv Filters .. .. .. .. .. 48 Python img.filter(CONTOUR) method .. .. .. .. 50 Python img.filter(DETAIL ) method .. .. .. . 52 Python img.filter(EDGE_ENHANCE) method .. .. .. . 54 Python img.filter(EDGE_ENHANCE_MORE) method .. .. 56 Python img.filter(EMBOSS) method .. .. .. .. 58 Python img.filter(FIND_EDGES) method .. .. .. . 60 Python img.filter(SMOOTH) method .. .. .. . 62 Python img.filter(SHARPEN) method .. .. .. 63 13. Python Pillow Colors on an Image .. .. .. 67 Color Names .. .. .. .. 67 ImageColor.getrgb() Method .. .. .. . 67 ImageColor. getcolor() Method .. .. .. 69 14. Python Pillow ImageDraw Module .. .. .. .. 71 Canvas .. .. .. .. 72 .. .. .. . 74 15. Python Pillow Image Sequences .. .. .. 80 16. Python Pillow Writing Text on Image .. .. .. .. 82 Selecting the font .. .. .. .. . 83 17. Python Pillow M L with Numpy .. .. .. .. 85 Creating image from Numpy Array .. .. .. . 85 Creating greyscale images .. .. .. .. 86 Creating numpy array from an Image .. .. .. .. 88

PAGE – 5 ============
Python Pillow 1 . In case , we are working with Python progra mming language, it provides lot of i mage processing libraries to add image processing capabilities to digital images. Some of the most common image processing libraries are: OpenCV, Python Imaging Library (PIL), Scikit – image, Pillow. However, in this tutorial, we a re only focusing on Pillow m odule and will try to explore various capabilities of th is module. Pillow is built on top of PIL (Python Image Library). PIL is one of the important modules for image processing in P ython. However, the PIL module is not supported since 2011 and t python 3. Pillow module gives more functionalities, runs on all major operating system and support low module. Apart from basic image processing functionality, including point operations, filtering images using built – in convolution kernels, and color space conversions. Image Archives The Python Imaging Library is best suited for image archival and batch processing applications. Python pillow package can be used for creating thumbnails, converting from one format to another and print images , etc. Image Display You can display images using Tk Ph o toImage, BitmapImage and Windows DIB interface, which can be us ed with PythonWin and other Windows – based toolkits and many other Graphical User Interface ( GUI ) toolkits. For debugging purposes, there is a show () method to save the image to disk which calls the external display utility. Image Processing The Pillow lib rary contains all the basic image processing functionality. You can do image resizing, rotation and transformation. Pillow module allows you to pull some statistics data out of image using histogram method, which later can be used for statistical analysis and automatic contrast enhancement. 1. P ython Pillow Overview

PAGE – 6 ============
Python Pillow 2 This chapter discusses how to install pillow package in your computer. To install pillow using pip, just run the below command in your command prompt: python – m pip install pip python – m pip install pillow In case , if pip and pillow are already installed in your computer, above commands will requirement alre ady satisfied as shown below : 2. Python Pillow Environment Setup

PAGE – 10 ============
Python Pillow 6 >>> image = Image.open(‘beach1.jpg’) >>> >>> image.format ‘JPEG’ Image.mode >>> image.mode ‘RGB’ Image.size It returns the tuple consist of height & weight of the image . >>> image.size (1280, 721) Image.width It returns only the width of the image . >>> image.width 1 280 Image.height It returns only the height of the image . >>> image.height 721 Image.info It returns a dictionary holding data associated with the image. >>> image.info {‘jfif’: 257, ‘ jfif_version’: (1, 1), ‘dpi’: (300, 300), ‘jfif_unit’: 1, ‘jfif_density’: (300, 300), ‘exif’: b”Exif \ x00 \ x00MM \ x00* \ x00 \ x00 \ x00 . . \ xeb \ x00 \ x00′ \ x10 \ x00 \ x00 \ xd7 \ xb3 \ x00 \ x00 \ x03 \ xe8″}

431 KB – 95 Pages

By redy