我在 RAM 中有三個相同大小的 PIL 影像(這里沒有磁盤)。它們代表不同的頻率(細節、陰影、遮罩)。
是否可以將這些影像疊加到.xcf檔案中以在 gimp 中進一步處理它們?如果是這樣,可以在保存影像之前控制圖層的不透明度嗎?
理想情況下,我正在尋找 python 解決方案。
uj5u.com熱心網友回復:
不確定 GIMP 期望什么,但這可能會讓您從具有不同不透明度的多層 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432196.html
標籤:python-3.x python成像库 层 瘸子
上一篇:你如何使用dx?
