将原始数据保存为ti

2024-10-06 14:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要分析一个图像的一部分,作为子矩阵,在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")

Tags: 文件代码图像格式plt矩阵装饰标签
1条回答
网友
1楼 · 发布于 2024-10-06 14:22:26

首先,如果您只想存储原始值或原始值的灰度表示,那么使用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将对数组中的值进行插值,即使将插值设置为“最近”,保存的图像仍将是为图形指定的大小。

如果要使用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)

相关问题 更多 >