将原始数据保存为tif [英] Save raw data as tif

查看:741
本文介绍了将原始数据保存为tif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在tif文件中分析被选为子矩阵的图像的一部分。我想要原始格式的图像,没有多余的装饰(缩放,轴,标签等)...我怎么能这样做?



这是我现在使用的代码:

  submatrix = im [x_min:x_max,y_min:y_max] 
plt.imshow (子矩阵)
plt.savefig(subplot_%03i_%03i.tif%(index,peak_number),format =tif)


解决方案

首先,如果您只想存储原始值或原始值的灰度表示,最简单的方法就是使用PIL为此。



例如,这将生成一个10x10灰度级的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版本有更多像素的问题,这是因为你暗示告诉它。 Matplotlib数字有一个大小(以英寸为单位)和一个dpi(默认情况下,屏幕上为80,保存时为100)。此外,默认情况下, imshow 将插入数组中的值,即使将插值设置为最近,保存的图像仍将是您为图形指定的大小。 / p>

如果你想使用matplotlib将图形保存在一个像素到一个像素(例如,为了方便使用色彩图),做类似的事情这个:

  import numpy as np 
import matplotlib.pyplot as plt

dpi = 80#任意。图像中的像素数将始终相同
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 )


I need to analyze a part of an image, selected as a submatrix, in a tif file. I would like to have the image in raw format, with no frills (scaling, axis, labels and so on)... How could I do that?

This is the code I am using now:

 submatrix = im[x_min:x_max, y_min:y_max]
 plt.imshow(submatrix)
 plt.savefig("subplot_%03i_%03i.tif" % (index, peak_number), format = "tif")

解决方案

First off, if you're just wanting to store the raw values or a grayscale representation of the raw values, it's easiest to just use PIL for this.

For example, this will generate a 10x10 grayscale tif file:

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')

As far as your question about why the matplotlib version has more pixels, it's because you implictly told it to. Matplotlib figures have a size (in inches) and a dpi (by default, 80 on-screen and 100 when saved). Also, by default imshow will interpolate the values in your array, and even if you set interpolation to nearest, the saved image will still be the size you specified for the figure.

If you want to use matplotlib to save the figure at one-value-to-one-pixel (for example, to allow easy use of colormaps), do something similar to this:

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)

这篇关于将原始数据保存为tif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆