您可以像这样使用 PIL 和 Numpy 来做到这一点 - 我倾向于使用 Numpy,因为它更快、更灵活:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Open input image and ensure it is RGB
im = Image.open('start.png').convert('RGB')
# Make into Numpy array
imnp = np.array(im)
# Split into 3 constituent bands
r = imnp[:, :, 0]
g = imnp[:, :, 1]
b = imnp[:, :, 2]
# Process
g = 2*g - r - b
# Recombine to single image and save
merged = np.dstack((r, g, b))
Image.fromarray(merged).save('result.png')
或者您可以不那么明确地拆分并在整个图像上就地执行:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Open input image and ensure it is RGB
im = Image.open('start.png').convert('RGB')
# Make into Numpy array
imnp = np.array(im)
# Process
imnp[:,:,1] = 2*imnp[:,:,1] - imnp[:,:,0] - imnp[:,:,2]
# Save
Image.fromarray(imnp).save('result2.png')
关键字:Python、Numpy、PIL、Pillow、颜色矩阵、颜色矩阵、变换、乘通道、缩放通道、分离、单独、单个通道、波段、组件、单独、图像、图像处理。