Python Pillow is a Python Imaging Library. This library supports many file formats, has a fast internal representation, and performs complex image processing. The popular tools of Python is Pillow. It supports images such as jpeg, jpg, gif, bmp, ppm, and tiff.
Installing Pillow using pip
python -m pip install pillow
Opening, rotating and displaying an image
Opening an image is a basic operation of the image processing. We import the image module from the Pillow library to load the image. It provides the Image.open() method.
Example of Opening , rotating and displaying an image
from PIL import Image
#Open image using Image module
im = Image.open(r””)
#Show actual Image
im.show()
#Show rotated Image
imim = im.rotate(90)
im.show(
Attributes of Image
- Image.filename
- Image.format
- Image.mode
- Image.size
- Image.width
- Image.height
- Image.info
- Image.palette
Image File Name
This function is use to get the file name or the path name of the image is knows as Image File Name.
Example of Image File Name
image = Image.open('rose.jpg') image.filename 'rose.jpg'
Image Format
This function returns file format of the image file Just like jpeg, jpg, gif, bmp, ppm, and tiff.
Example of Images Format
image = Image.open('Rose1.PNG') >>> >>> image.format 'PNG'
Image Mode
It is use to get the pixel format used by the image is knows as Images mode.
Example of Image mode
image.mode ''
Images Size
It returns the tuple consist of height and weight of the image is knows as Images Size.
Example of Images Size
image.size (1101, 1005)
Image Width
It returns only the width of the image is knows as Images Width.
Example of Images Width
image.width 1101
Image Height
It returns only the height of the image is knows as Images Height.
Example of Images height
image.height 1005
Image info
It returns a dictionary holding data associated with the image is knows as Images info.
Example of Images info
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"}
Image palette
Example of Images Palette
image.palette
Working with Images
Reading an Image
Reading and writing images using pillow library is very Easy, with the help of PIL.Image module function.
Syntax
Image.open(fp, mode=’r’)
Example of Reading an Image
from PIL import Image image = Image.open('rose.jpg') image.show() image.save('rose.bmp') image1 = Image.open('rose.bmp') image1.show()
Saving an Image
The save() function writes an image to file. is knows as Saving an images
Syntax
Image.save(fp, format=None, **params)
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about Pillow Library Function please Wikipedia Click here
Stay Connected Stay Safe, Thank you
0 Comments