1. 程式人生 > >Image.open 和scipy.misc.imread開啟讀取圖片,以及torch的型別

Image.open 和scipy.misc.imread開啟讀取圖片,以及torch的型別

tensorflow,numpy的順序是(batch,h,w,c)

pytorch的順序是(batch,c,h,w)

裡面的圖片開啟屬性檢視,可以知道(h,w)=(375,1242)      高度(h),寬度(w)

用Image開啟的a是一個PIL型別的,用另外一個開啟的b是numpy型別的

所以a檢視大小:a.size=(1242,375)=(w,h),注意a沒有shape屬性,另外一個b檢視大小:b.shape=(375,1242,3)=(h,w,c),注意b.size=1397250=375x1232x3

a.resize((w,h),Image.BILINEAR),相對的scipy.misc.imresize(b,(h,w),interp='nearest')

from PIL import Image
import scipy.misc
a=Image.open('/home/zzp/um_lane_000000.png')
b=scipy.misc.imread('/home/zzp/um_lane_000000.png')
print(type(a))
print(type(b))
print(a)
print(b)
c=np.array(a).astype(np.float32).shape # 先將PIL型別轉化成numpy型別
print(c)
d=c.transpose((2,0,1))
print(d)
e=torch.from_numpy(d).float()    # 再將numpy型別轉化成torch.tensor型別
print(type(e))

# the following are outputs
#  type(a)=<class 'PIL.PngImagePlugin.PngImageFile'>  
#  type(b)=<class 'numpy.ndarray'>   
#  a=<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1242x375 at 0x7F11F0D0FB70> # = a
#  b=是一個(375,1242,3)的具體展開
#  c = (375,1242,3)=(h,w,c), type(c)=type(b)=type(d)
#  d = (3,375,1242)轉換成一個Torch的張量形式(c,h,w)
#  type(e) = torch.tensor, size=(3,375,1242)
a=Image.open('/home/zzp/um_lane_000000.png').convert('RGB')
b=scipy.misc.imread('/home/zzp/um_lane_000000.png',mode='RGB')