Python Forum
Error message with PIL putpixel
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
Reply
#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
Reply
#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"...
Reply
#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
Reply
#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 ?
Reply
#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
Reply
#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 ^^
Reply
#8
(Mar-30-2018, 05:36 PM)louloudevinci Wrote: I had replace y by x, it's work !
In almost all graphical programs, "x" is left-to-right, and is the first number, and "y" is the second, being top-to-bottom. So when you do something like my_image.size, and it returns a tuple of (number_one, number_two), number_one is almost guaranteed to be the x-coordinate.

With that in mind, let's look at your code.
(Mar-30-2018, 04:39 PM)louloudevinci Wrote:
largeur, longueur = picture_1.size
 
for y in range(largeur) :
    for x in range(longueur) :
Let's show your code again, but with re-named variables:
max_x, max_y = picture_1.size
 
for y in range(max_x) :
    for x in range(max_y) :
You're getting an index out of range error, because you're swapping the meaning of x and y. If your image was square (say, 40x40 pixels), there would be no error.
Reply


Forum Jump:

User Panel Messages

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