Analysis of operation difference of Matplotlib, PIL and CV2 images

Source: Internet
Author: User

In the image processing using Python, we used three kinds of libraries, such as Matplotlib.pyplot, PiL and Cv2, and the methods of reading and saving of three kinds of library images were different, and the order of image reading was different, such as plt. Imread and PIL.Image.open are read in RGB order, and Cv2.imread read in BGR order . You need to be more careful when using.

Now refer to Http://www.jianshu.com/p/3977d674da85. The three kinds of library image reading and saving were combed. There is a certain difference from the original reference resources. Currently used as the python3.5 version.

reading Images

1.matplotlib.pyplot

Matplotlib read in the picture is unit8,0-255 range.

2.pil.image.open

PIL has its own data structure, but it can be converted into numpy arrays, and the converted array is unit8,0-255

3.cv2.imread

OpenCV read in is the NumPy array, the type is uint8,0-255.

4.plt. Imread and PIL.Image.open are all read in RGB order, and Cv2.imread read in BGR order . You need to be more careful when using.

displaying images

All with Plt.imshow (IMG): Because OPENCV read in the BGR order, and imshow need RGB order, so you need to turn plt.imshow (img[..., -1::-1]) first.

Save Image

1 Pil.image-Save the picture in PIL format

Img.save ("1.jpg")

2.cv2.imwrite-Save the picture in numpy format

Cv2.imwrite ("1.jpg")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.