Image中resize函數報錯:ValueError:image has wrong mode

img_pil_u500 = np.array(img_uint500)
im_u500 = Image.fromarray(img_pil_u500) 
im_u500r = im_u500.resize((W,H),Image.ANTIALIAS)  
im_u500r.save(outputpath+str(n)+'-raw_u500pil.tif')

因爲img_uint500是np.uint16的
第三句,resample的參數只能默認爲Image.NEAAREST纔不會報錯
一旦修改就會報錯:
ValueError: image has wrong mode
但是在插值中NEAREST雖然性能最好,但是效果最差
爲了不丟失掉圖片中的信息,最好選一個根據自己的需要選擇插值算法

解決方法:
將img_uint500的數據類型轉成np.uint32的話,上述代碼就能運行
生成的tif圖片可以直接用scipy.misc讀取,得到的數據類型是int32

個人理解的原因:
The mode of an image defines the type and depth of a pixel in the image. The current release supports the following standard modes:
1 (1-bit pixels, black and white, stored with one pixel per byte)
L (8-bit pixels, black and white)
P (8-bit pixels, mapped to any other mode using a color palette)
RGB (3x8-bit pixels, true color)
RGBA (4x8-bit pixels, true color with transparency mask)
CMYK (4x8-bit pixels, color separation)
YCbCr (3x8-bit pixels, color video format)
Note that this refers to the JPEG, and not the ITU-R BT.2020, standard
LAB (3x8-bit pixels, the Lab color space)
HSV (3x8-bit pixels, Hue, Saturation, Value color space)
I (32-bit signed integer pixels)
F (32-bit floating point pixels)

Image的mode中並不支持16bit的pixels

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章