Python - 多个图像到多层 .xcf

Python - Multiple images to multilayered .xcf

我在 RAM 中有三个相同大小的 PIL 图像(这里没有磁盘)。它们代表不同的频率(细节、阴影、蒙版)。

是否可以将这些图像叠加到 .xcf 文件中,以便在 gimp 中进一步手动处理它们?如果是这样,可以在保存图像之前控制图层的不透明度吗?

我正在寻找 python 解决方案。

不确定 GIMP 期望什么,但这可能会让您开始使用具有不同透明度的 multi-layer TIFF:

#!/usr/bin/env python3

import numpy as np
from PIL import Image

w, h = 400, 400

# Our layers to write to output file
layers = []

# Make layer 0 - 400x400 RGBA with red square, A=64
im = np.full((w,h,4), [0,0,0,64], np.uint8)
im[10:110, 10:110, :3] = [255,0,0]
layers.append(Image.fromarray(im))

# Make layer 1 - 400x400 RGBA with green square, A=128
im = np.full((w,h,4), [0,0,0,128], np.uint8)
im[20:120, 20:120, :3] = [0,255,0]
layers.append(Image.fromarray(im))

# Make layer 2 - 400x400 RGBA with blue square, A=192
im = np.full((w,h,4), [0,0,0,192], np.uint8)
im[30:130, 30:130, :3] = [0,0,255]
layers.append(Image.fromarray(im))

# Save as multi-layer TIFF with PIL
layers[0].save('result.tif', save_all=True, append_images=layers[1:], compression='tiff_lzw')

macOS 上的 Preview 应用程序显示如下 - 希望您至少可以看到红色更透明,或者更不鲜艳:

tiffinfo 是这样理解的:

TIFF Directory at offset 0x9740 (260c)
  Image Width: 400 Image Length: 400
  Bits/Sample: 8
  Compression Scheme: LZW
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 40
  Planar Configuration: single image plane
TIFF Directory at offset 0x20096 (4e80)
  Image Width: 400 Image Length: 400
  Bits/Sample: 8
  Compression Scheme: LZW
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 40
  Planar Configuration: single image plane
TIFF Directory at offset 0x30506 (772a)
  Image Width: 400 Image Length: 400
  Bits/Sample: 8
  Compression Scheme: LZW
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 40
  Planar Configuration: single image plane