【问题标题】:Reading and saving tif images with python使用python读取和保存tif图像
【发布时间】:2021-08-30 04:38:36
【问题描述】:

我正在尝试使用 python 读取this tiff 图像。我已尝试 PIL 并保存此图像。该过程进展顺利,但输出图像似乎很暗。这是我使用的代码。

from PIL import Image

im = Image.open('file.tif')
imarray = np.array(im)

data = Image.fromarray(imarray)
    
data.save('x.tif')

如果我做错了什么,或者是否有任何其他工作方式可以读取和保存 tif 图像,请告诉我。我主要需要它作为 NumPy 数组进行处理。

【问题讨论】:

  • 您链接的是 PNG 图像,而不是 TIFF 图像...
  • 链接图片为 PNG。如果你想读取 TIFF 图像,我建议你使用 rasterio 或 gdal。
  • 网站自动转换了。现在我更新了链接。
  • 根据 ImageMagick,您的 TIFF 是 TIFF 1024x1024 1024x1024+0+0 12-bit Grayscale Gray 1574650B - 我认为 PIL 可能无法读取 12 位灰度图像(更重要的是按原样写回它们)。正如@PabloSilió 所说,也许可以试试rasterio
  • 谁能帮我写代码?

标签: python image-processing python-imaging-library


【解决方案1】:

问题只是图像很暗。如果你用 PIL 打开它,然后转换为 Numpy 数组,你可以看到最大亮度为 2455,在可能范围为 0..65535 的 16 位图像上,意味着它只有 2455/65535,或 3.7% 亮度.

from PIL import Image

# Open image
im = Image.open('5 atm_gain 80_C001H001S0001000025.tif')

# Make into Numpy array
na = np.array(im)

print(na.max())       # prints 2455

因此,您需要对图像进行标准化或提高亮度。一个非常粗略的方法是乘以 50,例如:

Image.fromarray(na*50).show()

但实际上,您应该使用适当的规范化,例如 PIL.ImageOps.autocontrast() 或 OpenCV normalize()

【讨论】:

    猜你喜欢
    • 2021-05-29
    • 2018-11-26
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2022-01-27
    • 1970-01-01
    • 2019-06-13
    相关资源
    最近更新 更多