gpt4 book ai didi

python - 如何使用pillow的python lib实现图像 channel 下降

转载 作者:行者123 更新时间:2023-12-01 00:54:49 24 4
gpt4 key购买 nike

我需要随机将枕头图像的 0、1 或 2 个 channel 归零。这意味着我需要将图像的 0、1 或 2 个 channel 随机设置为 0。

我怎样才能用 pil 做到这一点?

最佳答案

这是一种简单的原生 PIL 方法,通过乘以变换来实现此目的。我设置了默认变换,所以数学看起来像这样:

newRed   = 1*oldRed  +  0*oldGreen  +  0*oldBlue  + constant
newGreen = 0*oldRed + 1*OldGreen + 0*OldBlue + constant
newBlue = 0*oldRed + 0*OldGreen + 1*OldBlue + constant

然后我只需将 1 更改为 0,我希望 channel 归零。

#!/usr/bin/env python3

from PIL import Image

# Open image
im = Image.open('input.png').convert("RGB")

# Pre-set R, G and B multipliers to 1
Rmult, Gmult, Bmult = 1, 1, 1

# Select one (or more) channels to zero out, I choose B channel here
Bmult=0

# Make transform matrix
Matrix = ( Rmult, 0, 0, 0,
0, Gmult, 0, 0,
0, 0, Bmult, 0)

# Apply transform and save
im = im.convert("RGB", Matrix)
im.save('result.png')

所以,如果你从这个开始:

enter image description here

并将蓝色乘数 (Bmult) 设置为零,您将得到:

enter image description here

如果将红色和蓝色归零:

Rmult = Bmult = 0 

你会得到:

enter image description here

关于python - 如何使用pillow的python lib实现图像 channel 下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com