6

此功能的想法是仅淡化图片的上半部分(使其逐渐变暗)。这是我所拥有的,但它似乎使所有上半部分都是纯黑色的。

def fadeDownFromBlack(pic1):

w=getWidth(pic1)
h=getHeight(pic1)

for y in range(0,h/2):
     for x in range(0,w):
        px=getPixel(pic1,x,y) 
        setBlue(px,y*(2.0/h)) 
        setRed(px,y*(2.0/h)) 
        setGreen(px,y*(2.0/h))
4

4 回答 4

4

要使像素变暗,请将红色、绿色和蓝色级别乘以适当的分数。

你在做什么:

setBlue(px,y*(2.0/h))

你被告知要做的事情:

setBlue(px,y*(2.0/h) * getBlue(px))
于 2010-02-11T00:19:44.053 回答
3

让我们在这里只看一行:

setBlue(px,y*(2.0/h))

这里的关键部分是

y*(2.0/h)

y 会随着您下降而发生变化。让我们尝试一些简单的 y 和 h 值。假设 h 为 100,我们将检查 y 何时为 0 和 50 (h/2)。对于 y = 0,我们得到 0。对于 y = 50,我们得到 1。如果您的颜色值范围是 256,而 0 是最暗的,那么难怪这是黑色。您所拥有的是从 0. 到 1. 的值范围,但我猜您想要的是将该数字乘以旧颜色值。

你想要的是:

setBlue(px,y*(2.0/h)*getBlue(px))

其他颜色也有类似的东西。

于 2010-02-11T00:16:23.470 回答
2

找出 setBlue/Red/Green 方法的比例。我假设 0 对应于黑色,但最亮的是什么?你似乎假设它是 1,但它实际上可能是 255 或其他东西。即使它是 1,看起来这段代码也没有考虑像素的旧值,它只是根据其垂直位置将其设置为精确的颜色。也许这就是你想要的,但我对此表示怀疑。您可能希望将像素的当前值乘以某个值。

于 2010-02-10T23:20:44.050 回答
2

只是为了分享一个增强版并添加一些视觉效果(因为视觉效果很好)......

# 'divisor' : How much we expand the gradient (less is more)
# 'switch' : If True, start gradient from bottom to top
def fadeDownFromBlack(pic, divisor, switch=False):  

   w = getWidth(pic)
   h = getHeight(pic)


   startY = 0
   endY = min(h-1, int(h/float(divisor)))
   inc = 1

   if (switch):
     startY = h-1
     endY = max(0, h-1 - int(h/float(divisor)))
     inc = -1

   color_ratio = float(divisor)/h

   for y in range(startY, endY, inc): 
       for x in range(0,w):
           px = getPixel(pic, x, y )
           setRed(px, abs(startY - y)*(color_ratio)*getRed(px))
           setGreen(px, abs(startY - y)*(color_ratio)*getGreen(px))
           setBlue(px, abs(startY - y)*(color_ratio)*getBlue(px))


file = pickAFile()
picture = makePicture(file)
# The following commented line answers the question
#fadeDownFromBlack(picture, 2)
fadeDownFromBlack(picture, 0.7, True)

writePictureTo(picture, "/home/mad-king.png")

show(picture)


输出(Cornelu Baba的绘画- 疯王):


………………………………………………………………………………………………………………………………………………………… 在此处输入图像描述_ 在此处输入图像描述_


于 2013-06-29T09:53:00.223 回答