PIL(RGB)
from PIL import Image
import numpy as np
image = Image.open('test.jpg') # 图片是400x300 宽x高
print type(image) # out: PIL.JpegImagePlugin.JpegImageFile
print image.size # out: (400,300)
print image.mode # out: 'RGB'
print image.getpixel((0,0)) # out: (143, 198, 201)
# resize w*h
image = image.resize((200,100),Image.NEAREST)
print image.size # out: (200,100)
image = np.array(image,dtype=np.float32) # image = np.array(image)默认是uint8
print image.shape # out: (100, 200, 3)
image=image.astype(np.float32)
Image.open 打开的图片类型为PIL Image, 值为0-255,尺寸为 W`*H*C,通过img=np.array(img)转为numpy数组,尺寸为H*W*C
Skimage(RGB)
import skimagefrom skimage
import io,transform
import numpy as np
image= io.imread('test.jpg',as_grey=False)# 第一个参数是文件名可以是网络地址,第二个参数默认为False,True时为灰度图
print type(image) # out: numpy.ndarray
print image.dtype # out: dtype('uint8')
print image.shape # out: (300, 400, 3) (h,w,c)前面介绍了ndarray的特点# mode也是RGB
print image
'''
注意此时image里都是整数uint8,范围[0-255]
array([
[ [143, 198, 201 (dim=3)],[143, 198, 201],... (w=200)],
[ [143, 198, 201],[143, 198, 201],... ], ...(h=100) ], dtype=uint8)
'''
image = io.imread('test.jpg',as_grey=False)# h*w
image = transform.resize(image,(100, 200),order=1) # order默认是1,双线性#resize后image范围又变成[0-1]
print image.dtype # out: dtype('float64')
print image.shape # out: (100, 200, 3)
print image
'''
array([[
[0.56078431, 0.77647059, 0.78823529],
[ 0.56078431, 0.77647059, 0.78823529],
[ 0.56078431, 0.77647059, 0.78823529], ..., ...]])
'''
'''
resize函数接口
resize(image, output_shape, order=1, mode='constant', cval=0, clip=True, preserve_range=False)
order : int, optional
The order of interpolation. The order has to be in the range 0-5:
- 0: Nearest-neighbor
- 1: Bi-linear (default)
- 2: Bi-quadratic
- 3: Bi-cubic
- 4: Bi-quartic
- 5: Bi-quintic
'''
print skimage.img_as_float(image).dtype # out: float64
# img_as_float可以把image转为double,即float64
skimage.io.read 打开的图片类型为np数组, 值为0-255,尺寸为 H*W*C,且resize后值为0-1
OpenCV(python版)(BGR)
import cv2
import numpy as np
image = cv2.imread('test.jpg')
print type(image) # out: numpy.ndarray
print image.dtype # out: dtype('uint8')
print image.shape # out: (300, 400, 3) (h,w,c) 和skimage类似
print image # BGR
'''
array([
[ [143, 198, 201 (dim=3)],[143, 198, 201],... (w=200)],
[ [143, 198, 201],[143, 198, 201],... ],
...(h=100)
], dtype=uint8)
'''
# w*h
image = cv2.resize(image,(100,200),interpolation=cv2.INTER_LINEAR)
print image.dtype # out: dtype('uint8')
print image.shape # out: (200, 100, 3)
'''
注意注意注意 和skimage不同
resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
关键字参数为dst,fx,fy,interpolation
dst为缩放后的图像
dsize为(w,h),但是image是(h,w,c)
fx,fy为图像x,y方向的缩放比例,
interplolation为缩放时的插值方式,有三种插值方式:
cv2.INTER_AREA:使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN方法
cv2.INTER_CUBIC: 立方插值
cv2.INTER_LINEAR: 双线形插值
cv2.INTER_NN: 最近邻插值
[详细可查看该博客](http://www.tuicool.com/articles/rq6fIn)
'''
'''
cv2.imread(filename, flags=None):
flag:
cv2.IMREAD_COLOR 1: Loads a color image. Any transparency of image will be neglected. It is the default flag. 正常的3通道图
cv2.IMREAD_GRAYSCALE 0: Loads image in grayscale mode 单通道灰度图
cv2.IMREAD_UNCHANGED -1: Loads image as such including alpha channel 4通道图
注意: 默认应该是cv2.IMREAD_COLOR,如果你cv2.imread('gray.png'),虽然图片是灰度图,但是读入后会是3个通道值一样的3通道图片
‘’‘
cv2.imread 打开的图片类型为np数组, 值为0-255,尺寸为 HWC