如何使用PIL删除RGB中基于发光的像素?

时间:2013-12-18 20:01:50

标签: python image colors python-imaging-library

我正在尝试编写一个小故障艺术程序,让我拍摄的照片有点像沙子,我让它适用于灰度(L)。我正在尝试将其转换为颜色,但我无法使用此代码。这就是我的颜色

#! /usr/bin/python
from PIL import Image
from random import randint

# Sorts img kinda randomly
source = Image.open("test.jpg")
threshold = 150

img = source.load()

blackandwhite = source.convert("L").load()

canvas = Image.new("RGB", source.size)

newimg = canvas.load()
count = source.size[0]

print (source.format)
print (source.size)
print (source.mode)
print ("Threshold: ", threshold)
print ("============================================")

counter = 0 #counter
# do the loop twice because we want to make em fall!
counter = 0
for i in range(0, source.size[0]-1): # loop through every x value
    vert_list = [] #list of this column
    for pix in range(0, source.size[1]-1): #make a list of the column from the b&w img
        color = blackandwhite[i, pix] #for being in color ^^
        vert_list.append( color )

    counter += 1
    if counter % 10 == 0:
        print(counter, "/", count)
    #now remove all pixels brighter than the threshold

    color_vert_list = []
    for x in range(0, len(vert_list)-1):
        if vert_list[x] < threshold:
            color_vert_list.append(img[i, pix]) #add colors darker than something to the color list

    top_spacing = source.size[1] - len(color_vert_list) #height
    for pixel in range(0, len(color_vert_list)):
        newimg[i,pixel + top_spacing] = color_vert_list[pixel] #add em


canvas.save("fall.png") #save

这里的代码与黑白相同

#! /usr/bin/python
from PIL import Image
from random import randint

# Sorts img kinda randomly
source = Image.open("test.jpg")
threshold = 150

img = source.load()

blackandwhite = source.convert("L").load()

canvas = Image.new("L", source.size)

newimg = canvas.load()
count = source.size[0]

print (source.format)
print (source.size)
print (source.mode)
print ("Threshold: ", threshold)
print ("============================================")

counter = 0 #counter
# do the loop twice because we want to make em fall!
counter = 0
for i in range(0, source.size[0]-1): # loop through every x value
    vert_list = [] #list of this column
    for pix in range(0, source.size[1]-1): #make a list of the column from the b&w img
        color = blackandwhite[i, pix] #for being in color ^^
        vert_list.append( color )

    counter += 1
    if counter % 10 == 0:
        print(counter, "/", count)
    #now remove all pixels brighter than the threshold
    vert_list[:] = (x for x in vert_list if threshold > x)

    top_spacing = source.size[1] - len(vert_list) #height
    for pixel in range(0, len(vert_list)):
        newimg[i,pixel + top_spacing] = vert_list[pixel]


canvas.save("fall.png")

1 个答案:

答案 0 :(得分:1)

看起来你的问题是你试图从灰度值的索引中找回颜色值(你已经丢弃了),但是当你已经丢弃了之前的灰度值时,它就无法工作了并且指数不再匹配。

让我们重新开始吧。不要保留灰度顶点列表和颜色顶点列表并尝试匹配它们,我们只需保留(灰度,颜色)对的列表。像这样:

for pix in range(0, source.size[1]-1): #make a list of the column from the b&w img
    grey = blackandwhite[i, pix]
    color = img[i, pix]
    vert_list.append((grey, color))

现在,您只需要更改过滤器以使用对中的灰色值:

vert_list[:] = (x for x in vert_list if threshold > x[0])

...并更改newimg分配以使用对中的颜色值:

newimg[i,pixel + top_spacing] = vert_list[pixel][1]

(除了改变你已经正确的canvas的模式),这就是你需要做的全部。

我发布了一个完整的实现here(对三条更改的行进行了评论)。