Python Forum
random change of color pixel with PIL
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random change of color pixel with PIL
#1
Hey !
I wrote this code to randomly change the colors pixels of an image.
The first time, it's good, pixels are randomly changed but after this, it'ls always the same image and pixels...
Can you tell me why ?

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()
Reply
#2
Hello, hopefully I understand what you want to do - create a new image with the same size as panda.jpg but color of every pixel is randomly generated, so:
        (r, g, b) = picture_1.getpixel((x,y))
What is this line for?

Here:
r = randint(0,25) ;
v = randint(0,255) ;
b = randint(0,255)
Semicolons should be removed and also you have variable called v?
Reply
#3
Hey ! You understood me !

(r, g, b) = picture_1.getpixel((x,y))
This line is for get the values of the r, g and b for all the pixels of the image.
I don't need this for change the value after ?

And, I'm french, so "v" is for "vert" which means "green" ^^
I changed it with a "g", but now, my image is just a lot pixels everywheeeeeere...

So, if i change only the r and g for exemple, the image stay correct, but the random thing doesn't work...

Anyway, thanks for your help, i must find an other solution...
Reply
#4
(r, g, b) = picture_1.getpixel((x,y)
You don't need color of pixel from your original picture because you dont actually use them

So altogether, with loop changed to this:
for x in range(largeur) :
    for y in range(longueur) :
        r = randint(0,25)
        g = randint(0,255)
        b = randint(0,255)
        picture_2.putpixel((x,y), (r, g, b))
it will create a new image with the same size as your original picture and every pixel will have random color.

Or is this not actually what you wanted? :D
Reply
#5
Indeed ^^
I wanted to change randomly the color of my image, to get the same image with different colors, but i have no idea how to do this now...
I can just change two variables, it works, but I would like to change them all randomly...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Voxel to pixel graphics voicemail 3 563 Nov-19-2023, 09:45 AM
Last Post: paul18fr
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 981 Oct-25-2023, 09:09 AM
Last Post: codelab
  Pixel color and action Sartre 4 2,054 Apr-13-2023, 03:26 AM
Last Post: Sartre
  Dynamic pixel display jerryf 2 717 Mar-17-2023, 07:26 PM
Last Post: jerryf
  simplekml change shape&color issac_n 2 2,811 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  Plotting Pixel Intensity Doev 0 1,715 Oct-21-2020, 10:56 PM
Last Post: Doev
  Change the color automatically Dragonos 5 2,820 Jul-28-2020, 01:17 PM
Last Post: Dragonos
  Use of input function to change screen background color in Turtles Oldman45 3 4,822 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,580 May-15-2020, 12:37 PM
Last Post: scidam
  Fast get pixel and oparate. storzo 7 7,055 Aug-26-2019, 07:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020