2013-12-18 28 views
0

我想寫一個故障的藝術計劃,讓我讓圖片有點像沙子一樣掉下來,我讓它爲灰度(L)工作。我試圖將其轉換爲彩色,但我無法獲得此代碼的工作。下面是我對色彩如何根據PIL中RGB的發光去除像素?

#! /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") 
+0

你是什麼意思「我有它爲RGB工作,我試圖把它轉換成彩色」? RGB是一種顏色格式。 L是_not_。所以,如果你有它的RGB工作......你完成了。 – abarnert

+0

我讓它在灰度下工作,只有Luminsecence值。我試圖達到相同的結果,但使最終的圖像使用原始的RGB值。編輯我的帖子,以反映這一點。 –

+0

好吧,它有什麼不對(理想情況下,哪裏出了問題,你期望在那裏發生什麼不同)? – abarnert

回答

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(對三條改變的行註釋)。

+0

太棒了,完美的工作!謝謝 :) –