PIL - Python Imaging Library
- Details
- Last Updated: Sunday, 13 September 2020 05:27
- Published: Sunday, 13 September 2020 00:16
- Hits: 996
PIL/Pillow: PIL = Python Imaging Library.
PIL is free library that allows to process images in various formats, as JPEG, PNG, BMP, etc. However, PIL's last commit was in 2011, and supports Python 1.5 only. Since there was no active development of PIL for Python3, a fork of PIL called PILLOW was released for supporting Python3. PILLOW wanted to completely replace PIL, that is why even though, we install PILLOW, we import it as PIL in python pgm. All the cmds, functions, modules etc in pillow are exactly the same as in PIL, so to end user there is no difference seen. It still looks like PIL is installed, and he can keep on working w/o any changes. All tutorials for PIL work for PILLOW also, since everything is the same. But keep in mind, that there is no PIL for python3, only Pillow. We should not install PIL, just pillow for any version of python going forward. If you had installed PIL, uninstall it, and install pillow as shown below. Keep in mind, that pillow is what we should learn, not PIL.
Pillow installation:
$ sudo python3.6 -m pip install pillow => installs Pillow as PIL. Here we are installing Pillow for python3.6
/usr/local/lib64/python3.6/site-packages/PIL/* => We see PIL dir, and lots of files under it, as well as 2 Pillow files that provides some additional info
ex: import pillow as PIL => Here we import pillow library as PIL, so that in our pgm, as just keep using PIL as if nothing changed.
Other Imaging libraries: There are other imaging libraries as openCV, Matplotlib, etc. "Matplotlib" tis mostly for plotting, but works on images too. However, Pillow looks more complete in terms of working on most types of images, and Matplotlib documentation also says that it falls back on Pillow for cases where it doesn't have inbuilt support. So, I would invest time in learning Pillow for working on images.
Good tutorial here:
https://www.tutorialspoint.com/python_pillow/index.htm
Syntax:
Pillow library uses image class. There are lot of functions/methods that we can use on Image class.
1. methods open, save, show, resize, etc: These m ethods of class Image are used to open the image, save it, show it and to resize to some other size:
ex: test.py
from PIL import Image # Here we import Image module from Pillow library (even though we say PIL, it loads Pillow if Pillow is installed. However, if PIL is installed, then it will load from PIL, which is not what we want. So, we will have to uninstall PIL and install Pillow)
im = Image.open("images/cuba.jpg") #We use open function to load image into image class "im". We provide path starting from current dir, or we can provide the full path too.
im.show() #This shows image loaded above using show() method
im = im.rotate(45) #rotate image using rotate method
im.show() #Show rotated Image
im.save('beach1.bmp') #This saves the "im" image class as beach1.bmp (in bmp format). We can also specify the format, else it's inferred from file extension. Note that original image is still intact, as we saved with different file name.
resized_image = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.25))=> this resizes the image to 1/2 for height and 1/4 for width. Or we can provide the size explicitly as im.resize(64,64). This is useful in AI, where we want to work with a standard size image.
resized_image.save('resizedBeach1.jpg') #this saves the resized image
2. using with numpy: image class above can be used with numpy module. We can cnvert image into numpy array and vice versa. This is very useful in AI.
A. convert array into image: fromarray() => used for creating image with numpy array:
ex: below ex creates a 150 by 250-pixel array (height=150, width=250) then fill left half of the array with orange and right half of the array with blue.
from PIL import Image
import numpy as np
arr = np.zeros([150, 250, 3], dtype=np.uint8) #creates a numpy array object of shape (150,250,3) filled with all 0. The innermost triplet holds the R,G,B colors.
arr[: , :125] = [255, 128, 0] # This slicing says for axis=0 choose all range (i.e basically all rows in picture since outermost array is for rows or height). then for axis=1, it selects range from 0 to 125 (it chooses cols 0 to 100 for width. To all these array slice (which happens to be left half), it assigns RGB value as [255,128,0] which corresponds to orange
arr[: ,125:] = [0, 0, 255] #same as above, except that for axis=1, range is from 125 to max (which hapens to be right half). It assigns RGB to blue only
img = Image.fromarray(arr) #forms an image object from given 3D array
img.show()
img.save("RGB_image.jpg")
B. convert image into array: array() => used to extract image pixels from a picture and convert them into numpy array
ex:
im = Image.open('my_image.jpg')
image_arr = np.array(im) #returns in H X W X C
arr_image = np.array(im) # we can pass this object to numpy array func (explained in numpy module section). So, arr_image becomes a 3D array of height X width X color
print(arr_image.ndim, arr_image.shape, arr_image) => prints "dim=3 shape=(240, 160, 3) arr_image=[ [ [20 47 90] .... [45 67 102] ] ]. so this picture has 240 pixels in height, 160 pixels in width and 3 values for color corresponding to R,G,B
img = Image.fromarray(arr_image) #from above array, form the picture again
img.show() #display orig image
3. misc functions available for plenty of operations on images. Look in tutorialspoints website.