在Python中,常用的读图工具包括opencv、PIL(pillow)、tensorflow、mxnet等,下面我们将举例说明:
假定图片路径为
img_path = './img/0001.jpg'
opencv
import cv2
import numpy as np
# method 1
image1 = cv2.imread(img_path)
# method 2
image2 = cv2.imdecode(np.fromfile(img_path, dtype = np.uint8), -1)
可以用np.equal(image1, image2).all()
判定两种方法得到的数据是完全相同的。
PIL.Image
from PIL import Image
import numpy as np
# method 3
image3 = Image.open(img_path)
image3 = np.array(image3)
tensorflow
import tensorflow as tf
# method 4
image4 = tf.gfile.FastGFile(img_path, 'rb').read()
image4 = tf.image.decode_jpeg(image4)
mxnet
import mxnet as mx
# method 5
image5 = mx.image.imdecode(open(img_path, 'rb').read())
image5 = image5[:, :, ::-1].asnumpy()
skimage.io
from skimage import io
# method 6
image6 = io.imread(img_path)
image6 = image6[:, :, ::-1]