【问题标题】:What is the conversion to save a jpg image from a numpy array?从 numpy 数组保存 jpg 图像的转换是什么?
【发布时间】:2021-11-16 12:05:14
【问题描述】:

我正在使用 hamamatsu 相机,我得到一个 NumPy 数组,我想像图像一样保存数组,我可以将它保存为 TIF 图像,但我不知道如何将 TIF 图像或数组转换为得到一个正确的 jpg 图像,我有这个代码:

img = Image.fromarray(self.val_fin)

    if int(self.vTIFF.get()) == 1:
        imgTIFF = img.convert('I')
        img.save('name1.tiff')

    if int(self.vJPG.get()) == 1:
        imgJPG = img.convert('RGB')
        imgJPG.save('name2.jpg')

其中val_fin是一个32bit的数组,其负值已经变为0,jpg图像的结果是黑色图像。 谢谢。

【问题讨论】:

  • 您有一台滨松相机...您没有指定其型号。您收到一个 Numpy 数组...大概来自相机...其尺寸 dtypeminimamaxima 以及您未指定的通道数...并且您想制作 JPEG .请尝试更清楚,以便我们可以更好地帮助您。谢谢。
  • 将图像缩小到16bit,如果失败再试一次尝试转换为8bit深度再试一次
  • @MarkSetchell 你有没有一个滨松相机样本文件,我找到了这个hamamatsu.com/sp/sys/en/camera_simulator/index.html,但下载了一个png!!

标签: python python-imaging-library tiff


【解决方案1】:

使用 tiff 32bites 浮动图像:

我可以运行这段代码:

from PIL import Image

import numpy as np


def normalize8(I):
  mn = I.min()
  mx = I.max()

  mx -= mn

  I = ((I - mn)/mx) * 255.0
  return np.round(I).astype(np.uint8)

img1 = Image.open('test_fl.tif', 'r')

arr = np.asarray(img1)

print(arr.size, arr.shape, arr.ndim , arr.dtype)


img = Image.fromarray(arr, mode='F')
print(img.size, img.format, img.mode)
img.save('test_saved.tif')

# doesnt work
# imgTIFF = img.convert(mode='I')
# imgTIFF.save('name1.tif')
# img2 = Image.open('name1.tif', 'r')
# print(img2.size, img2.format, img2.mode)

imgTIFF = Image.fromarray(normalize8(arr))
imgTIFF.save('name1.tif')
img2 = Image.open('name1.tif', 'r')
print(img2.size, img2.format, img2.mode)



imgJPG = imgTIFF.convert('RGB')
imgJPG.save('name2.jpg')

img3 = Image.open('name2.jpg')
print(img3.size, img3.format, img3.mode)
img3.show()

print(img3.getpixel((0,0)))

取自How should I convert a float32 image to an uint8 image?

imgTIFF = img.convert(mode='I')

尝试将 tiff 32float 转换为 32int 也会给我一个黑色图像

【讨论】:

    猜你喜欢
    • 2019-01-21
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多