is that is free and open-source an additional library for the programming language that adds support for , , and in a variety of extension. Pillow Python Imaging Library Python opening manipulating saving Let’s get started The most important class in the is the class, defined in the module with the same name. Python Imaging Library Image We use to image in our as shown below; open ( ) open local directory >>> PIL Image >>> sample = Image.open( ) from import 'sample.jpg' That’s simple, you have already read an image with , you can now do something with it, You can also check the type of the image we just loaded; pillow image processing >>> type(sample) < ' . . '> class PIL JpegImagePlugin JpegImageFile You can view your such , , and as shown below image property format size color mode >>> sample.format >>> sample.size ( , ) >>> sample.mode 'JPEG' 250 283 'RGB' Also, you can the you have just read to the screen, using the show method display image >>> sample.show() Output Image Format Conversion Once you are done opening your image with the in one format you can save it again in other formats like from to and much more. pillow library jpg png For instance, let’s try to write a simple Python program to on your project directory which are in format to format. convert all images jpg png os sys PIL Image jpg_images = [image image os.listdir() image.endswith( )] jpg_image jpg_images: : new_name = jpg_image.split( )[ ] + Image.open(jpg_image).save(new_name) except IOError error: print( .format(jpg_image)) import import from import for in if '.jpg' for in try '.' 0 '.png' as 'Couldn\'t read {} ' Once you run the above code on a project directory consisting of images in jpg format, it gonna open all of them and convert into png just as shown below, you could repeat the same process for other conversions. Image Cropping can also be used to perform whereby you can a of a given image by specifying of the sub rectangle. Pillow image cropping extract sub rectangle coordinates Therefore before you perform , you might wanna know the area of the rectangle to get a of coordinates you would like crop, you can easily observe them by showing to the screen. cropping pair >>> PIL Image >>> picture = Image.open( ) >>> cord = ( , , , ) >>> new_picture = picture.crop(cord) >>> new_picture.show() from import 'sample.png' 50 50 300 300 Output As we can see the image has been successful cropped. The coordinates of the surface to be cropped are represented by a . diagonal coordinates Whereby the first two-point is from the upper left diagonal point and next two-point are also the diagonal point from the bottom right. (x, y) (x2, y2) Geometrical Transformation With we can perform some transformation on our image include image size and rotations. pillow geometrical resizing This knowledge plays a great role when for by transforming one image to form other tons of images from different angles. generating data deep learning Image resizing >>> PIL Image >>> image = Image.open( ) >>> resized_image = image.resize(( , )) >>> resized_image.save( ) from import 'sample.png' 200 200 'resized.png' When you run the above code you should see a newly resized image on your local directory with a dimension of . 200 x 200 Image Rotation >>> PIL Image >>> image = Image.open( ) >>> rotated_img = image.rotate( ) >>> rotated_img.save( ) from import 'sample.png' 80 'rotated_img.png' Use to generate of the picture at different angles, doing this way will help us generate as many as data which you can potentially use to train your . rotation feature 360 images same deep learning model Image generator.py PIL Image images = [ ] img images: : org_img = Image.open(img) angle range( , ): image_name = str(angle)+ new_img = org_img.rotate(angle) new_img.save(image_name) except IOError: print( .format(img)) from import 'sample.jpg' for in try for in 1 361 '.jpg' 'Couldn\'t read {}' After running the above script, you should see images of the same original image at different angles as shown below. 360 Image Filtering is a technique for or an image. For example, you can an image to certain features or remove other features. Filtering modifying enhancing filter emphasize is useful for many applications including , , , and . Image filtering smoothing sharpening removing noise edge detection There many filters available on Pillow Library including BLUR, BoxBlur, CONTOUR, FIND_EDGES, Filter, GaussianBlur, Kernel, MaxFilter, MedianFilter, SHARPEN, SMOOTH and etc. Example of Usage. Let’s try finding edges in the below picture using the filter FIND_EDGES Sample image >>> PIL Image >>> PIL Image, ImageFilter >>> image = Image.open( ) >>> edges = image.filter(ImageFilter.FIND_EDGES) >>> edges.show() from import from import 'brain.jpeg' Output Same wise you can experiments with different in Python depending on what you’re trying wanna do. filters Reading Image From open file Also, you can use a pillow to read an Image from a Python file object as shown below; >>> PIL Image >>> image = Image.open(open( , )) from import 'brain.jpeg' 'rb' Reading Image from URL On this you will have to use Pillow in combination with requests, requests will have to send a get to the server to get the of image and the pillow will read those bytes. requests raw bytes >>> requests >>> PIL Image >>> url = >>> raw = requests.get(url, stream=True).raw >>> Image.open(raw).show() import from import 'https://i1.wp.com/kalebujordan.com/wp-content/uploads/2020/04/code-1084923.png' Output Creating new Images With Pillow you can also create a new blank image, of you might need for a variety of purposes, use to generate an entirely new image. Image.new ( ) Syntax = Image.new(mode, shape, color) new Example of Usage : >>> PIL Image >>> new_img = Image.new( , ( , ), ) >>> new_img.show() from import 'RGB' 500 500 'blue' Drawing Rectangles on Images Pillow can also be used to draw Rectangle on Images, this commonly applied on object detection whereby you can draw a rectangular box over a detected object. Example of Usage Let’s try drawing a rectangular box inside a blank image >>> PIL Image, ImageDraw >>> new_img = Image.new( , ( , ), ) >>> pencil = ImageDraw.Draw(new_img) >>> pencil.rectangle(( , , , ), fill = ) >>> new_img.show() from import 'RGB' 400 400 'black' 200 50 300 300 'green' Output The first two coordinates represent (x,y ) of the left upper diagonal and the next two (x2, y2) represent the coordinate point of the bottom right. Drawing Text on Images We can also use Pillow Library to draw Text on Images PIL Image , ImageDraw, ImageFont new_img = Image.new( , ( , ), ) font = ImageFont.load_default() pencil = ImageDraw.Draw(new_img) pencil.text(( , ), , font=font, fill= ) new_img.show() >>> from import >>> 'RGB' 200 200 'black' >>> >>> >>> 100 100 'Hello World' 'blue' >>> Output Well that's all for today, hope you find it useful Now don't be shy, share it with your fellow developer The can be found on kalebujordan.com Original Article In case of any suggestion, comment, or difficulty drop it in community and I will get back to you ASAP.