简体   繁体   中英

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. Same initial seed, and same sequence of random number requests will yield the same 'random picture'.

For more detail, see random.seed .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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