我需要分析tif文件中選定爲子矩陣的圖像的一部分。我想以原始格式顯示圖像,沒有裝飾(縮放,軸,標籤等)......我怎麼能這樣做?將原始數據保存爲tif
這是我現在使用的代碼:
submatrix = im[x_min:x_max, y_min:y_max]
plt.imshow(submatrix)
plt.savefig("subplot_%03i_%03i.tif" % (index, peak_number), format = "tif")
我需要分析tif文件中選定爲子矩陣的圖像的一部分。我想以原始格式顯示圖像,沒有裝飾(縮放,軸,標籤等)......我怎麼能這樣做?將原始數據保存爲tif
這是我現在使用的代碼:
submatrix = im[x_min:x_max, y_min:y_max]
plt.imshow(submatrix)
plt.savefig("subplot_%03i_%03i.tif" % (index, peak_number), format = "tif")
首先,如果你只是想存儲原始值或原始值的灰度表現,這是最簡單的只使用PIL進行這個。
例如,這將產生一個10×10灰度TIF文件:
import numpy as np
import Image
data = np.random.randint(0, 255, (10,10)).astype(np.uint8)
im = Image.fromarray(data)
im.save('test.tif')
至於你爲什麼在matplotlib版本有更多的像素問題,那是因爲你implictly告訴它。 Matplotlib數字有一個尺寸(以英寸爲單位)和一個dpi(默認情況下,屏幕上顯示80,保存時顯示100)。此外,默認imshow
將插值數組中的值,即使將插值設置爲最接近,保存的圖像仍然是您爲該圖指定的大小。
如果你想使用matplotlib保存在一個價值對一個像素的數字(例如,以方便使用色彩映射的),做一些與此類似:
import numpy as np
import matplotlib.pyplot as plt
dpi = 80 # Arbitrary. The number of pixels in the image will always be identical
data = np.random.random((10, 10))
height, width = np.array(data.shape, dtype=float)/dpi
fig = plt.figure(figsize=(width, height), dpi=dpi)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(data, interpolation='none')
fig.savefig('test.tif', dpi=dpi)
我做了由Joe建議的更改,我得到以下錯誤:「ValueError:太多值解包」高度,寬度= np.array(data.shape,dtype = float)/ dpi。我應該改變什麼? –
@albus_c - 這聽起來像你有一個3D數組? 「data.shape」看起來像什麼?如果它是一個3D數組,你會想要做一些像'height,width,nbands = ...'。 –
@albus_c - 另外,如果你正在處理多波段圖像,直接使用'Image'(a.k.a.「PIL」)將它保存爲tif。我假設你想根據色彩圖(也可以用PIL來完成,也就是用PIL來完成單色圖像的着色,但構建色彩圖更困難)。 –
我猜想'plt'是'matplotlib.pyplot'。 –
有可能是有用的[這裏](http://stackoverflow.com/questions/8218608/scipy-savefig-without-frames-axes-only-content) –
可能重複的[Matplotlib情節:去除軸,傳說和空白](http://stackoverflow.com/questions/9295026/matplotlib-plots-removing-axis-legends-and-white-spaces) –