设置RGB像素级别(Python,Jython)

时间:2010-01-28 21:47:48

标签: python jython

   For each pixel in pic:
    r= random()      
    if r < 0.25:       
    set the red level to randrange(0,256), 
    set the green level to randrange(0,256) 
    set the blue level to randrange(0,256)

剩下的看不见的代码是正确的,我只是无法弄清楚如何很好地表达这个功能以便它起作用。

2 个答案:

答案 0 :(得分:1)

我对你的其余代码一无所知,但它会是这样的:

import random

for pixel in pic.get_pixels(): # Replace with appropiate way of getting the pixels
    if random.random() < 0.25:
        pixel.red = random.randint(256)
        pixel.green = random.randint(256)
        pixel.blue = random.randint(256)

同样,我不知道如何获得像素列表,或者如何为每个像素设置RGB值,但结果将是这样的。

答案 1 :(得分:0)

你在使用PIL吗?

如果是这样,一个选择是做这样的事情


your_image = Image.new("RGB", (512, 512), "white")
for x in xrange(your_image.size[0]):
    for y in xrange(your_image.size[1]):
        your_image.putpixel((x,y),(random.randint(256), random.randint(256), random.randint(256))

哦......我看到你明白了。好吧,我会发布这个。