Image processing in Python using the pillow (PIL) library
This article will show how to work with images using Pillow in Python. We will discuss basic operations like creating, saving, and rotating images. So let’s start discussing in detail but first, let’s see how to install a pillow.
Installation
To install this package type the below command in the terminal.
pip install pillow
After installing the pillow module once briefly, check the documentation for better knowledge and better understanding, and click on documentation for the document.
Creating new image
You can create a new image using PIL.Image.new() method. This method creates a new image with the given mode and size. Size is given as a (width, height)-tuple, in pixels
Syntax:PIL.Image.new(mode,size,color)
Code:
from PIL import Image image = Image.new("RGB",(200, 200), color="blue") image.show() image.save("test_image.png")
Output:
Opening image
You can open any image using PIL.Image.open(). Method.
Syntax: PIL.Image.open(fp,mode='r')
Code:
from PIL import Image image = Image.open("test_image.png") image.show()
Output:
Getting Information about the image
Getting the format of the image: obj. Format method returns the format of the image file.
from PIL import Image img = Image.open("test_image.png") print(img.format)
Output:
PNG
Getting the image size: obj.size attribute provides the size of the image. It returns a tuple that contains width and height.
from PIL import Image img = Image.open("test_image.png") print(img.size)
Output:
(200, 200)
Renaming and Saving Image
We can change the name, and format of the image, and can also rename it using an image.save(). Method.
Syntax:Image.save(fp, format=None, **params)
Code:
from PIL import Image image = Image.open('test.jpg') image.show() image.save(test1.bmp') image1 = Image.open('nature1.bmp') image1.show()
Output:
Before changing the name and format of the image
After Changing the name and format of the image
Cropping the image
PIL is the Python imaging library that provides the Python interpreter with image editing capabilities. PIL.Image.Crop() method is used to crop a rectangular portion of any image.
Syntax : PIL.Image.crop(box = None)
Code:
from PIL import Image image = Image.open(test.jpeg") image.show() image = image.crop((0,0,150,150)) image.show()
Output:
Rotating the image
PIL.Image.rotate() method is used to rotate a given image to the given number of degrees counterclockwise around its center.
Syntax:new_object = PIL.Image.Image.rotate(image_object, angle, resample = 0 , expand=0) OR new_object = image_object.rotate(angle resample = 0, expand = 0)
Code:
from PIL import Image im = Image.open("test.jpeg") im.show() im = im.rotate(45) im.show()
Output:
Filtering the image
The current version of the pillow library provides the below-mentioned set of predefined image enhancement filters.
- BLUR
- CONTOUR
- DETAIL
- EDGE_ENHANCE
- EDGE_ENHANCE_MORE
- EMBOSS
- FIND_EDGES
- SHARPEN
- SMOOTH
- SMOOTH_MORE
The ImageFilter module contains definitions for a pre-defined set of filters, that can be used with the image.filter() method.
Syntax: Filter(Kernel)
Code:
from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) img= Image.open('test.jpeg') img1 = img.filter(SHARPEN) img1.show()
Output:
Creating a watermark on the image
Here, you can create a watermark by using ImageDraw.Draw.text(). This method draws the string at the given position.
Syntax: ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor = None, spacing = 0, align="left")
Code:
from PIL import Image, ImageDraw, ImageFont im = Image.open('test.jpeg') draw = ImageDraw.Draw(im) text = "lovely Tree" font = ImageFont.truetype('arial.ttf',36) text_bbox = draw.textbbox((0,0),text, font=font) textwidth, textheight = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1] x = 100 y = 50 draw.text((x,y),text, font= font, fill="white") im.show() im.save('watermark.jpg')
Output: