Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error message with PIL putpixel
#1
Hey ! I want to change all the pixels of an image with this loop but I have an error message : "'module' object has not attribute 'putpixel' and I don't know why...

from PIL import Image

picture = Image.open("K:\profil\Bureau\CODES ISN\panda.jpg")
(largeur, longueur) = picture.size
print(largeur,"*",longueur)

for y in range(largeur) :
    for x in range(longueur) :
        newpicture = Image.putpixel(picture, (x,y), (116, 208, 241))

newpicture.show
Quote
#2
I think it has to be

newpicture = picture.putpixel(picture, (x,y), (116, 208, 241))
But don't you modifying the pixel in place using putpixel method? Why do you assign the return to an object?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#3
Because it's more simple ??? I don't know in fact...
I replaced by what you sow but an other message appear : "putpixel()takes 3 positional arguments but 4 were given"...
Quote
#4
https://pillow.readthedocs.io/en/3.1.x/r...e.putpixel

Since this is instance method you don't have to provide the same object as an argument. It takes only the coordinates and the colour.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#5
Oh, I see.
Thanks !
I change a little bit my code like this :
from PIL import Image

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

largeur, longueur = picture_1.size

print(largeur,"*",longueur)

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

for y in range(largeur) :
    for x in range(longueur) :
        picture_1.getpixel((x,y))
        r = 154 ; v = 152 ; b = 100
        picture_2.putpixel((x,y), (r, v, b))

picture_2.save("test2.jpg")
picture_2.show
but know, a message appear : "image index out of range" did you know why ?
Quote
#6
Just guessing.
Are the image coordinates start from 0?
Put

print(x,y)
break
just just below the second for loop and run the script to see what will come out.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#7
Oh, so my coordonates start always from zero...The y are not here.
Why...

I had replace y by x, it's work !
Thanks for your help ^u^
Oh, and sorry for my bad english, I'm french ^^
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)