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
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Sending/Receiving Multiple Message from Server or Client Lyperion 0 136 Jul-30-2018, 07:52 AM
Last Post: Lyperion

Forum Jump:


Users browsing this thread: 1 Guest(s)