Search code examples
pythonarraysimagepngpixel

Converting PNG file to bitmap array in Python


I would like to convert a PNG image to a 2 dimensional array where each array holds a list of the RGB values of that specific pixel. How could one create a program to read-in a *.png file and convert to this type of data structure?


Solution

  • If you have PIL installed then you can create an image with Image.open and get the colors like so:

    data = [image.getpixel((x, y)) for x in range(image.width) for y in range(image.height)]