Python 按通道修改RGB像素
我已经编写了一些Python/Pillow代码来生成一个联系人表,其中显示了一个图像的不同版本,其中通道强度由一系列因素调整。在这种情况下,每个颜色通道3个。是否可以不使用if/elif语句编写此代码,因为它看起来有点笨拙。我知道你可以使用numpy数组,但我很想知道是否有python+枕头的方法Python 按通道修改RGB像素,python,python-imaging-library,Python,Python Imaging Library,我已经编写了一些Python/Pillow代码来生成一个联系人表,其中显示了一个图像的不同版本,其中通道强度由一系列因素调整。在这种情况下,每个颜色通道3个。是否可以不使用if/elif语句编写此代码,因为它看起来有点笨拙。我知道你可以使用numpy数组,但我很想知道是否有python+枕头的方法 file = "an image.jpg" img = Image.open(file).convert('RGB') image_list = [] # initialise
file = "an image.jpg"
img = Image.open(file).convert('RGB')
image_list = [] # initialise image list
RGBindex = [0, 1, 2] # used as iterators through the 3 channels (RGB)
factors = [0.1, 0.5, 0.9] # list of factors to apply to channel pixels
factor_count = 3
# create a list of images, split by channel, from the original image
source_array = img.split()
# now iterate through the channels (RGB) and the factors to apply to each and create a list of 9 modified images
for chan in RGBindex:
for x in factors:
# The point function updates the pixel values by the factor x
wip = source_array[chan].point(lambda i: i * x)
if chan == 0: # Red
workingimage = Image.merge("RGB", (wip, source_array[1], source_array[2])) # merge back with the other two channels
elif chan == 1: # Green
workingimage = Image.merge("RGB", (source_array[0], wip, source_array[2]))
else: # Blue
workingimage = Image.merge("RGB", (source_array[0], source_array[1], wip))
image_list.append(workingimage) # and add to the list
谢谢
凯文我不知道你到底想要达到什么目标。在我看来,本质上你的问题更多的是如何操作数组而不是操作图像,对吗?如果是这样的话,你能给出一个样本输入和期望的样本输出吗?我不知道你到底想要实现什么。在我看来,本质上你的问题更多的是如何操作数组而不是操作图像,对吗?如果是的话,你能给出一个样本输入和一个期望的样本输出吗?