简体   繁体   English

为什么我随机改变像素颜色不起作用? (带 PIL)

[英]Why my random change of pixel color doesn't work ? (with PIL)

I wrote this code to change randomly the pixels colors of my image.我编写了这段代码来随机更改图像的像素颜色。 The random change work one time, but it's all... Afet the first time, it's the same image over and over...随机变化一次起作用,但它全部......第一次,它一遍又一遍地重复相同的图像......

Can you give me a clew ?你能给我一个提示吗?

from PIL import Image
from random import randint

picture_1 = Image.open("panda.jpg")

largeur, longueur = picture_1.size

print(largeur,"*",longueur)

picture_2 = Image.new("RGB", (largeur, longueur))

for x in range(largeur) :
   for y in range(longueur) :
       (r, g, b) = picture_1.getpixel((x,y))
       r = randint(0,25) ; v = randint(0,255) ; b = randint(0,255)
       picture_2.putpixel((x,y), (r, g, b))


picture_2.save("pandatest6.jpg")
picture_2.show()

Try adding尝试添加

from random import randint, seed
seed()

at the beginning of your code.在代码的开头。 This will initialize the random number generator with current system time, ensuring a different image every time the code is run.这将使用当前系统时间初始化随机数生成器,确保每次运行代码时都有不同的图像。

The initial seed determines the sequence of random numbers your randint calls will receive.初始种子确定您的 randint 调用将收到的随机数序列。 Same initial seed, and same sequence of random number requests will yield the same 'random picture'.相同的初始种子和相同的随机数请求序列将产生相同的“随机图片”。

For more detail, see random.seed .有关更多详细信息,请参阅random.seed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM