将多通道 numpy 数组转换为 photoshop PSD 文件

Convert multi-channel numpy array to photoshop PSD file

我有一个带有 3 个 RGB 通道和两个 alpha 通道的 numpy 数组。 (我正在使用 python)

我想将其转换为 Photoshop .psd 文件,以便稍后在带注释的 alpha 层上应用 photoshop 中的转换。

我想这是一个非常简单的任务,但我还没有找到任何方法来从我通过谷歌搜索找到的包中完成它。

我猜应该是以下几行:

>> im.shape
(.., .., 4)

psd = PSD()
psd.add_layers_from_numpy(im, names=["R", "G", "B", "alpha-1", "alpha-2")
with open(of, 'wb') as f:
    psd.write(f)

如果你知道怎么做,请告诉我。 提前致谢!

我在 Photoshop 中打开了您的 PSD 文件并将其另存为 TIFF。然后我用 tiffinfo 检查并确定你的文件被保存为 RGB 有 3 层 "Extra samples":

tiffinfo MULTIPLE_ALPHA.tif

TIFF Directory at offset 0x8 (8)
  Subfile Type: (0 = 0x0)
  Image Width: 1000 Image Length: 1430
  Resolution: 72, 72 pixels/inch
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color                      <--- HERE
  Extra Samples: 3<unspecified, unspecified, unspecified>    <--- HERE
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 6
  Rows/Strip: 1430
  Planar Configuration: single image plane
  Software: Adobe Photoshop 21.2 (Macintosh)
  DateTime: 2020:09:08 19:34:38

您可以将其加载到 Python 中:

import tifffile
import numpy as np

# Load TIFF saved by Photoshop
im = tifffile.imread('MULTIPLE_ALPHA.tif')

# Check shape
print(im.shape)           # prints (1430, 1000, 6)

# Save with 'tifffile'
tifffile.imwrite('saved.tif', im, photometric='RGB')

现在检查 Photoshop 查看和处理 tifffile 图像是否与原始图像相同:

您可能想尝试使用 compress 参数。我注意到您的文件未压缩时为 8.5MB,但如果我使用 Zip-压缩数据,则为 2.4MB:

tifffile.imwrite('saved.tif', im, photometric='RGB', compress=1)

请注意,压缩 reading/writing 需要您安装 imagecodecs:

pip install imagecodecs

请注意,我并不是说 PSD-writer 包是不可能的,我只是说我相信你可以用 TIFF 得到你想要的东西。

关键词: 图像处理, Photoshop, PSD, TIFF, save multi-channel, multi-layer, multi-image, multiple alpha, tifffile, Python