欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PIL和cv2读取图片的区别

程序员文章站 2022-03-20 17:56:34
...

PIL读取图片

import os
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt

def load_image(image_name,data_dir,image_extension= '.jpg'):
    '''
    PIL或者plt读取图片是以RGB的格式显示
    '''
    path = os.path.join(data_dir,image_name + image_extension)
    image_PIL = np.asarray(Image.open(path).convert('RGB'))
    image_cv2 = cv2.imread(path)  #读取的直接是BGR
    return image_PIL
data_dir = 'C:/Users/Administrator/Desktop/python_learning/'
image_PIL= load_image('000001',data_dir)
plt.imshow(image_PIL)

显示结果:
PIL和cv2读取图片的区别
将图片转换成GBR

image_PIL_GBR = image_PIL[:,:,::-1].copy()
plt.imshow(image_PIL_GBR)

显示结果:
PIL和cv2读取图片的区别

cv2读取图片

import os
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
import cv2

def load_image(image_name,data_dir,image_extension= '.jpg'):
    '''
    cv2读取图片是以BGR的格式显示
    '''
    path = os.path.join(data_dir,image_name + image_extension)
    image_cv2 = cv2.imread(path)  #读取的直接是BGR
    return image_cv2
data_dir = 'C:/Users/Administrator/Desktop/python_learning/'
image_cv2 = load_image('000001',data_dir)
plt.imshow(image_cv2)

显示结果:
PIL和cv2读取图片的区别