首页 > 解决方案 > 使用 PIL 进行 RGB 处理 - 如何获取单个图像并生成具有不同 RGB 值的图像?

问题描述

我想拍一张照片,然后把它变成我添加到列表中的 9 张不同的图像。我想拍摄这张单张图像,然后将 R 值更改为一个较大的值,然后将 R 值更改为一半,最后将 R 值更改为一个较小的值。

我正在努力让它发挥作用。我的逻辑循环似乎不起作用,我的图像并没有被修改。任何帮助表示赞赏。

from PIL import Image

# Set the file and open it
file = "Final/charlie.png"
#pic = Image.open(file)

#Convert to RGB, so we dont have to deal with the alpha channel
#pic = pic.convert('RGB')
images = []
count = 0


#Image processing for lage change
def image_processing0(a):
    c = int(a / 10)
    return c

#Create PixelMap
count = 0
while count <3:
    pic = Image.open(file)
    pic = pic.convert('RGB')
    for x in range(pic.size[0]):
            for y in range(pic.size[1]):
                r,g,b = pic.getpixel((x,y))

#Check the count and use logic to appy the processing to the corect channel               
                if count == 1:
                    image_processing0(r)
                    pic.putpixel((x,y),(r,g,b))
                elif count == 2:
                    image_processing0(g)
                    pic.putpixel((x,y),(r,g,b))
                else:
                    image_processing0(b)
                    pic.putpixel((x,y),(r,g,b))
                    
    images.append(pic)  
    count+=1

标签: pythonimagepython-imaging-library

解决方案


您忘记分配 to 的返回image_processing0r, g, b

改变这个:

if count == 1:
    image_processing0(r)
    pic.putpixel((x,y),(r,g,b))
elif count == 2:
    image_processing0(g)
    pic.putpixel((x,y),(r,g,b))
else:
    image_processing0(b)
    pic.putpixel((x,y),(r,g,b))

到:

if count == 1:
    r = image_processing0(r)
    pic.putpixel((x,y),(r,g,b))
elif count == 2:
    g = image_processing0(g)
    pic.putpixel((x,y),(r,g,b))
else:
    b = image_processing0(b)
    pic.putpixel((x,y),(r,g,b))

推荐阅读