I mostly use OpenCV to complete my tasks as I find it 1.4 times quicker than PIL. First, let me show you step by step, how the image can be processed using both — OpenCV and PIL.
First of all, install the OpenCV Python package and import the package into Jupyter-Notebook or Python IDE.
pip install opencv-python
import cv2
Installation needed only for the 1st time usage.
Read the image into a variable
img = cv2.imread(path_to_the_image)
Use this method from the OpenCV package to read the image into a variable img
. As I mentioned earlier, OpenCV reads the image in BGR format by default.
What are BGR and RGB formats?
Both stand for the same colors (R) Red, (G) Green, (B) Blue but the order of arranging these colors areas is different. In BGR, the red color channel is considered as least important and in RGB, the blue color channel is the least important.
For the sake of this article, I will convert it to RGB format using the cvtColor
method.
Crop the Image
Let’s crop the image keeping the aspect ratio the same. So the area with the same aspect ratio will be cropped from the center of the image.
The aspect ratio of an image is the ratio of its width to its height. It is commonly expressed as two numbers separated by a colon, as in width:height. for example, 16:9.
In OpenCV, the image is a NumPy array and crops the image in the same way as NumPy array slicing. That’s why it is 8210X faster than PIL.
Rotate the Image
Image rotation is quite straightforward here. The method rotate()
in OpenCV allows image rotation in the multiples of 90°.
💡 More info about the method rotate()
can be found here.
Convert the Image to Grayscale
“Grayscale” image is an image that is composed of different shades of gray only, varying from black to white. An 8-bit image has 256 different shades of Gray color. Meaning, each pixel of the image, takes a value between 0 and 255. Again using the method cvtColor()
to convert the rotated image to the grayscale.
💡 All the color conversion codes can be found here.
Pillow is the currently used library, which is derived from PIL.
For the first-time usage, start with package installation. And then import the package into Jupyter-Notebook or Python IDE.
pip install Pillow
from PIL import Image, ImageEnhance
Read the image
Imported Image module has the method open()
which comes in handy while reading the image in PIL. Just like OpenCV, the image name with the extension or the entire path can be passed to this method.
Image Cropping with PIL
The same class Image has the method crop()
to crop the image. The image cropping with PIL is slightly different than OpenCV.
Image.crop()
takes a tuple as input. This tuple includes the coordinates for the upper left and the lower right corner of the area to be cropped.
Image Rotation with PIL
Image class includes also the method rotate()
. And unlike OpenCV, only the anticlockwise rotation angle as an integer can be passed to this method.
Convert the Image to Grayscale
Just like the other tasks in PIL, the image format conversion is also straightforward. The method convert()
returns the converted image.