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

1 投票
2 回答
2412 浏览
提问于 2025-04-15 18:36
   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

你在用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))

哦...我看到你已经解决了。不过我还是把这个发上来。

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值的,但结果大概会是这样的。

撰写回答