python中怎么存儲16bit和32bit的圖像,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

筆記:python中存儲16bit和32bit圖像的方法。
說明:主要是利用scipy庫和pillow庫,比較其中的不同。
import numpy as np
import scipy.misc
from PIL import Image
# 用已有的8bit和16bit圖作存儲測試
path26 = 'D:\Py_exercise\lena16.tif'
path8 = 'D:\Py_exercise\lena8.tif'
tif16 = scipy.misc.imread(path26) #<class 'numpy.uint16'>
tif8 = scipy.misc.imread(path8) #<class 'numpy.uint8'>
print(np.shape(tif16),type(tif16[0,0]))
print(np.shape(tif8),type(tif8[0,0]))
print()
save16 = 'D:\Py_exercise\lena16_save.tif'
save8 = 'D:\Py_exercise\lena8_save.tif'
scipy.misc.imsave(save16, tif16) #--> 8bit
scipy.misc.imsave(save8, tif8) #--> 8bit
# Create a mat which is 64 bit float
nrows = 512
ncols = 512
np.random.seed(12345)
y = np.random.randn(nrows, ncols)*65535 #<class 'numpy.float64'>
print(type(y[0,0]))
print()
# Convert y to 16 bit unsigned integers
z16 = (y.astype(np.uint16)) #<class 'numpy.uint16'>
print(type(z16[0,0]))
print()
# 用產(chǎn)生的隨機矩陣作存儲測試
save16 = 'D:\Py_exercise\lena16_save1.tif'
scipy.misc.imsave(save16, z16) #--> 8bit
im = Image.frombytes('I;16', (ncols,nrows), y.tostring())
im.save('D:\Py_exercise\lena16_save21.tif') #--> 16bit
im = Image.fromarray(y)
im.save('D:\Py_exercise\lena16_save22.tif') #--> 32bit
im = Image.fromarray(z16)
im.save('D:\Py_exercise\lena16_save23.tif') #--> 16bit
# 歸一化后的np.float64仍然存成了uint8
zNorm = (z16-np.min(z16))/(np.max(z16)-np.min(z16)) #<class 'numpy.float64'>
print(type(zNorm[0,0]))
save16 = 'D:\Py_exercise\lena16_save11.tif'
scipy.misc.imsave(save16, zNorm) #--> 8bit
# 歸一化后的np.float64直接轉(zhuǎn)8bit或16bit都會超出閾值,要*255或*65535
# 如果沒有astype的位數(shù)設(shè)置,會直接存成32bit
zImg = (zNorm*65535).astype(np.uint16)
im = Image.fromarray(zImg)
im.save('D:\Py_exercise\lena16_save31.tif') #--> 16bit
im = Image.fromarray(zNorm)
im.save('D:\Py_exercise\lena16_save32.tif') #--> 32bit(0~1)關(guān)于python中怎么存儲16bit和32bit的圖像問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
標題名稱:python中怎么存儲16bit和32bit的圖像-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://www.chinadenli.net/article14/dochde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、標簽優(yōu)化、商城網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站維護、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容