2013-10-30 60 views
3

我需要分析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") 
+0

我猜想'plt'是'matplotlib.pyplot'。 –

+1

有可能是有用的[这里](http://stackoverflow.com/questions/8218608/scipy-savefig-without-frames-axes-only-content) –

+0

可能重复的[Matplotlib情节:去除轴,传说和空白](http://stackoverflow.com/questions/9295026/matplotlib-plots-removing-axis-legends-and-white-spaces) –

回答

2

首先,如果你只是想存储原始值或原始值的灰度表现,这是最简单的只使用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) 
+0

我做了由Joe建议的更改,我得到以下错误:“ValueError:太多值解包”高度,宽度= np.array(data.shape,dtype = float)/ dpi。我应该改变什么? –

+0

@albus_c - 这听起来像你有一个3D数组? “data.shape”看起来像什么?如果它是一个3D数组,你会想要做一些像'height,width,nbands = ...'。 –

+0

@albus_c - 另外,如果你正在处理多波段图像,直接使用'Image'(a.k.a.“PIL”)将它保存为tif。我假设你想根据色彩图(也可以用PIL来完成,也就是用PIL来完成单色图像的着色,但构建色彩图更困难)。 –

相关问题