【发布时间】:2013-12-18 20:01:50
【问题描述】:
我正在尝试编写一个故障艺术程序,让我可以让图片有点像沙子一样掉下来,我让它适用于灰度 (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")
【问题讨论】:
-
“我让它为 RGB 工作。我正在尝试将其转换为颜色”是什么意思? RGB 是一种颜色格式。 L 是不是。所以如果你让它为 RGB 工作……你就完成了。
-
我让它在灰度下工作,只有 Luminsecence 值。我试图达到相同的结果,但使最终图像使用原始的 RGB 值。编辑我的帖子以反映这一点。
-
好的,它做错了什么(理想情况下,它哪里出错了,你期望在那里发生什么不同)?
-
我不确定出了什么问题。如果我使用灰度版本,我会得到预期的结果 Input Output ,但这是我在彩色模式下运行时得到的结果:i.imgur.com/J62KXCJ.png ,即使我期待与原始输出相同但颜色相同.我认为它在进行某种颜色转换时出错了?
标签: python image colors python-imaging-library