What libraries can load image in Python and what are their difference?

What libraries can load image in Python and what are their difference?Summarization & Comparison of .

imread()Kevin LukBlockedUnblockFollowFollowingMar 30When we face computer vision project, first of all we need to load the images before any preprocessing.

There are various libraries out there to perform imread() .

Here I want to consolidate the popular libraries for loading image and their difference.

This article will go through:Libraries for loading imageColour channelEfficiencyCheatsheet!Library for loading imageThere are four libraries that are usually used for loading images.

Matplotlib — plt.

imread()OpenCV — cv2.

imread()Pillow — Image.

open()scikit-image — io.

imread()import matplotlib.

pyplot as pltimg = plt.

imread(img_dir)import cv2img = cv2.

imread(img_dir)from PIL import Imageimg = Image.

open(img_dir)from skimage import ioimg = io.

imread(img_dir)Colour channelAfter loading the image, usually plt.

imshow(img) will be used to plot the images.

Let’s plot some doge!You may spot that the OpenCV image above looks odd.

It is because matplotlib, PIL and skimage represent image in RGB (Red, Green, Blue) order, while OpenCV is in reverse order!.(BGR — Blue, Green, Red)Easy FixJust convert the image from BGR to RGB using cv2.

cvtColor(img, cv2.

COLOR_BGR2RGB) before plotting using plt.

imshow() .

From blue to yellow dogenew_img = cv2.

cvtColor(img, cv2.

COLOR_BGR2RGB)plt.

imshow(new_img)EfficiencySo, you may ask which one is the most efficient library in loading the image.

Here a function is defined to track the time:import timedef test_read_image(imgfile, func): t0 = time.

time() img = func(imgfile) return img, time.

time() – t0The result is as follow:+————+————–+———+| Library | Function | Time |+————+————–+———+| matplotlib | plt.

imread() | 0.

02254 || OpenCV | cv2.

imread() | 0.

01096 || Pillow | Image.

Open() | 0.

00166 || Skimage | io.

imread() | 0.

01463 |+————+————–+———+Pillow — Image.

Open()seems to be the most efficient based on the result.

For further study, we may go back to the source code to find out more about the difference!CheatsheetI have combined the above information into a Jupyter Notebook.

Feel free to download the cheatsheet and happy coding!Source:https://blog.

csdn.

net/renelian1572/article/details/78761278https://github.

com/ZhangXinNan/LearnPractice/blob/master/cv/opencv/test_cvlib.

py.. More details

Leave a Reply