python - 创建 16 位 tiff 图像

标签 python python-imaging-library 16-bit

我正在尝试创建 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 位图像时,一切都会陷入困境。

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

最佳答案

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

在代码中

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

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

要创建 unsinged 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)
    
  • 您的处理可能会产生负数吗?将负数放入无符号整数数组时可能会导致另一个问题。

关于python - 创建 16 位 tiff 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70745715/

相关文章:

python - 当我运行 Flask 时,它显示错误 : ModuleNotFoundError: No module named 'werkzeug.contrib' . 谁能帮我解决这个问题?

python - 如何处理返回 NSError 的 PyObjC 方法?

python - 将作为二进制字符串加载的图像转换为 numpy 数组

python - PIL的getpixel和getdata的数据不一样?

python - 使用 Python 和 Pillow,我们如何计算 PNG 或 JPG 图像中唯一颜色的数量?

rest - 16 位 dBase 与 RESTful API 服务器集成

python - 获取具有其他两个绝对差异的数据框

python - pandas.shift 到底如何工作?

assembly - 从命令提示符读取字符并将其用作 8086 程序集中的路径名

c - GCC 裸机内联汇编 SI 寄存器与指针不能很好地配合