2010-01-28 37 views
1
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) 

看不見的代碼的其餘部分是正確的,我只是無法弄清楚如何將該功能短語足夠好以使其可以工作。設置像素的RGB級別(Python,Jython)

+0

「像素」的格式是什麼? – ezod 2010-01-28 21:56:33

+0

您可以通過正確縮進來開始。標籤和空格的混合並不是一個好主意... – Thomas 2010-01-28 21:56:51

+0

px在getPixels(圖片) – roger34 2010-01-28 22:03:21

回答

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值,但結果會是這樣的。

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)) 

哦......我看你得到了它。那麼我會發布這個反正。