【问题标题】:Pillow: converting a TIFF from greyscale 16 bit to 8 bit results in fully white image枕头:将 TIFF 从灰度 16 位转换为 8 位会产生全白图像
【发布时间】:2021-01-16 21:02:28
【问题描述】:

我知道关于 SO 有多个类似的问题,但我尝试了多个建议的解决方案均无济于事。

我有以下 TIFF 图像,在 Pillow 中打开为type='I;16'

Google Drive link

基于这个 SO question,我写了这段代码来转换它:

def tiff_force_8bit(image, **kwargs):
    if image.format == 'TIFF' and image.mode == 'I;16':
        array = np.array(image)
        normalized = (array.astype(np.uint16) - array.min()) * 255.0 / (array.max() - array.min())
        image = Image.fromarray(normalized.astype(np.uint8))

    return image

但是,结果是一个完全白色的图像。

我也尝试过其他解决方案,例如:

table = [i/256 for i in range(65536)]
image = image.point(table, 'L')

结果相同:全白。

谁能解释一下? 谢谢!

【问题讨论】:

  • 我的回答解决了你的问题吗?如果是这样,请考虑接受它作为您的答案 - 通过单击计票旁边的空心对勾/复选标记。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢。 meta.stackexchange.com/questions/5234/…
  • 嗨@MarkSetchell,我没有时间调查我的图像管道中的其他原因可能导致这种情况,但是鉴于您尝试了代码,我认为您的假设是正确的导致我的问题,所以我接受了你的回答。谢谢!

标签: image python-imaging-library tiff


【解决方案1】:

您的代码没有问题。如果你运行:

# Open image
im = Image.open('NGC 281 11-01-2021 Ha 1.15.tif')

# Force to 8-bit
res = tiff_force_8bit(im)

# Check min and max of result
res.getextrema()                           # prints (0,255) as expected

# Save as PNG
res.save('result.png')

# Display it
res.show()

我只能猜测你的安装或显示结果的方式有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多