【问题标题】:How do I remove pixels based on luminescence in RGB with PIL?如何使用 PIL 根据 RGB 中的发光去除像素?
【发布时间】: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


【解决方案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(在三个更改的行上有 cmets)。

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 2018-12-09
    • 2021-07-06
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多