Save raw data as tif

爱⌒轻易说出口 提交于 2019-12-04 04:07:54

问题


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

回答1:


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)


来源:https://stackoverflow.com/questions/19673619/save-raw-data-as-tif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!