我想寫一個故障的藝術計劃,讓我讓圖片有點像沙子一樣掉下來,我讓它爲灰度(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")
你是什麼意思「我有它爲RGB工作,我試圖把它轉換成彩色」? RGB是一種顏色格式。 L是_not_。所以,如果你有它的RGB工作......你完成了。 – abarnert
我讓它在灰度下工作,只有Luminsecence值。我試圖達到相同的結果,但使最終的圖像使用原始的RGB值。編輯我的帖子,以反映這一點。 –
好吧,它有什麼不對(理想情況下,哪裏出了問題,你期望在那裏發生什麼不同)? – abarnert