Python Forum
Error message with PIL putpixel - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Error message with PIL putpixel (/thread-9275.html)



Error message with PIL putpixel - louloudevinci - Mar-30-2018

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



RE: Error message with PIL putpixel - wavic - Mar-30-2018

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?


RE: Error message with PIL putpixel - louloudevinci - Mar-30-2018

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"...


RE: Error message with PIL putpixel - wavic - Mar-30-2018

https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.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.


RE: Error message with PIL putpixel - louloudevinci - Mar-30-2018

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 ?


RE: Error message with PIL putpixel - wavic - Mar-30-2018

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.


RE: Error message with PIL putpixel - louloudevinci - Mar-30-2018

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 ^^


RE: Error message with PIL putpixel - nilamo - May-08-2018

(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.