【问题标题】:creating a 16 bit tiff image创建 16 位 tiff 图像
【发布时间】:2022-01-18 02:53:00
【问题描述】:

我正在尝试创建一个 16 位灰度环模拟,但由于某种原因,它不起作用。

让我解释一下,一开始我是用 8 位格式编写的,然后我意识到我需要 16 位格式。我为 8 位格式编写的代码运行良好,如下所示:

from PIL import Image, ImageFilter
from scipy.ndimage import convolve, gaussian_filter
import NumPy as np

def theta(x, y, phi):
    if np.angle(x - y*1j, deg=True) - phi > 180:
        return 1*(np.angle(x - y*1j, deg=True) - phi - 360)
    if np.angle(x - y*1j, deg=True) - phi < -180:
        return 1*(np.angle(x - y*1j, deg=True) - phi + 360)
    else:
        return np.angle(x - y*1j, deg=True) - phi


#  FWHM = 2.355*Sigma

# Simulation Parameters:
Intensity = 190  # Light intensity.

SIG = 1.666/2.355  # Sigma of radial Gaussian.

SIG1 = 45  # Sigma of first azimuthal Gaussian.
SIG2 = 25  # Sigma of second azimuthal Gaussian.
SIG3 = 10  # Sigma of third azimuthal Gaussian.

r0 = 8  # Radius of reference of radial Gaussian.
theta1 = 31  # Angle of reference of first azimuthal Gaussian.
theta2 = 157  # Angle of reference of second azimuthal Gaussian.
theta3 = -105  # Angle of reference of third azimuthal Gaussian.

# PSF Parameters:
Kernel = MakeGaussian(10, 1.666)  # Convolution kernel.

# Noise Parameters:
offset = 1  # Gaussian noise amplitude.
Ex = 10  # Gaussian noise expectation. (3*Var)
Var = 7  # Gaussian noise variance.

# Frame Parameters:
t = 1  # Number of frames.
w, h = 300, 300  # Frame size.

data = np.zeros((t, h, w), dtype=np.uint8)
noise = np.zeros((t, h, w), dtype=np.uint8)

for l in range(t):
    for i in range(w):
        for k in range(h):
            r = np.sqrt((i - w / 2) ** 2 + (k - h / 2) ** 2)
            data[l][i][k] = Intensity * np.exp(-((r - r0)**2)/(2*SIG**2)) * 1 * (np.exp(-((theta(k - w / 2, i - h / 2, theta1))**2)/(2*SIG1**2)) + np.exp(-((theta(k - w / 2, i - h / 2, theta2))**2)/(2*SIG2**2)) + np.exp(-((theta(k - w / 2, i - h / 2, theta3))**2)/(2*SIG3**2)) )
            noise[l][i][k] = offset * (1/np.sqrt(2 * np.pi * Var**2)) * np.random.normal(Ex, Var)

    pic = gaussian_filter(data[l], 1.666, 0) + noise[l]

    img = Image.fromarray(pic, 'L')
    img.save('%s.tiff' % l, format="tiff")

现在,当我天真地试图通过切换到 dtype='uint.16' 来让这段代码创建 16 位图像时,一切都变得糟糕透顶。

如果有人能阐明我应该如何解决这个问题,我将不胜感激。

【问题讨论】:

    标签: python python-imaging-library 16-bit


    【解决方案1】:

    使用 PIL 保存 16 位无符号图像

    在代码中

    img = Image.fromarray(pic, 'L')
    

    根据 PIL 文档,“L”指定 8 位像素,黑白。

    要创建一个未分割的 int 16 位图像,需要 'I;16' 选项。

    img = Image.fromarray(pic[0], 'I;16')
    

    stackoverflow 帖子 Read 16-bit PNG image file using Python 说这个论点存在问题,但它对我使用 PIL 版本 8.2.0 和 Python 3.8.8 来说工作正常。

    其他注意事项

    • 您可能还需要小心处理数据和噪声数组。它们是无符号的 8 位整数。

      data = np.zeros((t, h, w), dtype=np.uint8)
      noise = np.zeros((t, h, w), dtype=np.uint8)
      

      可以使用 np.uint16 作为 dtype 参数将它们转换为无符号 16。

      data = np.zeros((t, h, w), dtype=np.uint16)
      noise = np.zeros((t, h, w), dtype=np.uint16)
      
    • 您的处理是否可以创建负数?将负数放入无符号整数数组时可能会导致另一个问题。

    【讨论】:

      【解决方案2】:

      正如this blog post 建议的那样,您需要外部库“libtiff”的帮助。由于 PIL 与 16 位作斗争。

      from libtiff import TIFF
      tiff = TIFF.open('libtiff.tiff', mode='w')
      tiff.write_image(ar)
      tiff.close()
      

      【讨论】:

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