0

I am currently working on a discord bot with Pycord. I am working on adding support for GIF images on the currently existing image commands, and I need the color of the pixels. When I try to get the color of an exact point in PIL/Pillow, I get a number representing the color of the pixel in the GIF color table, which is not what I want. Even when I convert the image to RGBA, I still get only the index, nothing else. When I google it, all I see is multitudes of this same method that I tried.

Here is a basic program to demonstrate what I have tried:

from io import BytesIO as toimg
from PIL import Image, ImageFont, ImageDraw, ImageOps, ImageSequence
      #reqdata is gif data from a url
      imggif = Image.open(toimg(reqdata.content))
      for frame in ImageSequence.Iterator(imggif):
        img = frame.convert("RGBA")
        img = img.convert("RGBA") # might not need this due to the line above but idk
        img = ImageOps.grayscale(img) # this line was not here before, edited it in.
        width, height = img.size
        for y in range(height):
          for x in range(width):
            print(img.getpixel((x,y))) # this prints out only one number, i need an RGBA value (4 numbers)

If anyone can help, that would be very appreciated!

Edit: I found out the solution and I realized that it was not the frame itself, but instead it was because I grayscaled the image after converting it. The program was created based on what I thought was the mistake, and I didn't even check it! This was nowhere in the question and i apologize for not thinking to look at such a simple thing before posting this question. I had to convert back to RGBA after grayscaling the image. :(

Edit 2: I am just now realizing that this is going to be my very last question, and that I should have looked further to realize my incredibly simple mistake before wasting my last chance on this site i will ever have. It's for the better, I'm a dumbass who is unable to realize such simple things. I will not be and have not been needed on this site.

3DG
  • 45
  • 2
  • 10
  • I just tried your code with a 24 frame animated GIF file, and `getpixel` always returned a 4 element tuple. The only difference is that I used a file rather than a byte sequence from a URL. – Mark Ransom Dec 10 '21 at 03:24
  • I just retried this by reading the entire file and passing it to `toimg` just as you did, and it still worked perfectly. If you expect some help, you'll need to produce code that allows *anyone* to reproduce your problem. – Mark Ransom Dec 10 '21 at 03:52
  • I literally just found out that it was the grayscaling that i didn't suspect/mention once in this question... Apologies for wasting everyones time over this mistake. – 3DG Dec 10 '21 at 19:55
  • That was an important detail to leave out. You could have had an answer immediately. – Mark Ransom Dec 10 '21 at 22:15
  • I am now aware, and I apologize once again for leaving such an important detail out of the question – 3DG Dec 11 '21 at 23:30
  • Personally I accept your apology - mistakes happen. Can't speak for anybody else though. – Mark Ransom Dec 12 '21 at 04:16
  • No need to be discouraged by a simple mistake - I would imagine you'd be more careful next time. – Mark Ransom Dec 24 '21 at 04:46
  • But now there is no "next time" because of that simple mistake. – 3DG Dec 24 '21 at 06:10
  • But the only reason there's no "next time" is because you choose it, right? You haven't been banned or anything, have you? If you ask another question, and it's a good one, nobody will remember or care about this one. – Mark Ransom Dec 24 '21 at 14:14
  • I've reached "the question limit" like, 3 times now. This time there was a notification saying that "some of my questions in the past were not well recieved, and i'm in danger of not being able to ask any more." Now I cannot ask any more questions on this account. I am also not knowledgeable enough in coding to answer questions, so I basically just threw this account in the trash. – 3DG Dec 24 '21 at 15:01
  • None of your questions have a negative score, so I'm baffled why you'd get that message. Have you actually tried to post another question and been rejected? You might get a reprieve by deleting some of those "not well received" questions - I've seen a lot of people do that. – Mark Ransom Dec 24 '21 at 15:07

2 Answers2

1

Try

r, g, b, a = img.getpixel((x, y))

I tested this and it works for me. Based on [this post]. (Get pixel's RGB using PIL)

Edit: another approach that has worked for me in the past is to use pixels = img.load() and index a pixel like pixels[x, y]

Juicestus
  • 442
  • 1
  • 7
  • 14
  • I got a typeerror with "cannot unpack non-iterable int object." – 3DG Dec 10 '21 at 02:55
  • @3DG my experience matches this answer as well. If it isn't working for you, then you didn't convert the image to RGBA as you claimed in your question. – Mark Ransom Dec 10 '21 at 03:10
0

This worked for me

    from PIL import Image
    
    red_image = Image.open("red.png")
    red_image_rgb = red_image.convert("RGB")
    rgb_pixel_value = red_image_rgb.getpixel((10,15))
    print(rgb_pixel_value) #Prints (255, 0, 0) 
Flip
  • 1
  • 1