2

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?

0

3 Answers 3

3

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)]
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the existing pygame module. Import a file into a Surface using pygame.image.load. You can then access the bit array from this using pygame.surfarray.array2d. Please see the Pygame docs for more information.

Comments

0

You can use wand for such basic tasks. The syntax is very easy to read unlike other ImageMagik libs. Basically you'd do something like:

from wand.image import Image
from wand.display import display

array = []
with Image(filename='yourfile.png') as img:
    array.append(img.channel_images)        # this is most likely wrong, but it should be something similar

It will be along those lines. Once I leave the office I will try this out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.