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)
显示结果:
将图片转换成GBR
image_PIL_GBR = image_PIL[:,:,::-1].copy()
plt.imshow(image_PIL_GBR)
显示结果:
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)
显示结果:
上一篇: WKWebView的使用(二)
下一篇: vue实现员工信息录入功能
推荐阅读
-
Linux上安装Python的PIL和Pillow库处理图片的实例教程
-
Linux上安装Python的PIL和Pillow库处理图片的实例教程
-
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
-
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
-
Pytorch使用PIL的读取单张图片并显示
-
php调用方法mssql_fetch_row、mssql_fetch_array、mssql_fetch_assoc和mssql_fetch_objcect读取数据的区别
-
Python 读取图片文件为矩阵和保存矩阵为图片的方法
-
Java 读取Word表格中的文本和图片
-
C#图片的读取和转码
-
Python实现批量修改图片格式和大小的方法【opencv库与PIL库】