Getting list of pixel values from PIL

前端 未结 9 1073
感动是毒
感动是毒 2020-11-27 15:07

Guys, I\'m looking for a bit of assistance. I\'m a newbie programmer and one of the problems I\'m having at the minute is trying to convert a black & white .jpg

相关标签:
9条回答
  • 2020-11-27 16:01

    Or if you want to count white or black pixels

    This is also a solution:

    from PIL import Image
    import operator
    
    img = Image.open("your_file.png").convert('1')
    black, white = img.getcolors()
    
    print black[0]
    print white[0]
    
    0 讨论(0)
  • 2020-11-27 16:03

    Looks like PILlow may have changed tostring() to tobytes(). When trying to extract RGBA pixels to get them into an OpenGL texture, the following worked for me (within the glTexImage2D call which I omit for brevity).

    from PIL import Image
    img = Image.open("mandrill.png").rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
    
    # use img.convert("RGBA").tobytes() as texels
    
    0 讨论(0)
  • 2020-11-27 16:07

    As I commented above, problem seems to be the conversion from PIL internal list format to a standard python list type. I've found that Image.tostring() is much faster, and depending on your needs it might be enough. In my case, I needed to calculate the CRC32 digest of image data, and it suited fine.

    If you need to perform more complex calculations, tom10 response involving numpy might be what you need.

    0 讨论(0)
提交回复
热议问题