0

I've been trying to convert an integer array of RGB values to a PNG image. How can I generate the following image from the following integer array?

enter image description here

'''This is a 3D integer array. Each 1D array inside this array is an RGBA value'''
'''Now how can I convert this RGB array to the PNG image shown above?'''
rgbArray = [
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]],
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]],
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]],
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]],
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]],
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]],
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]],
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]],
]
11
  • 1
    And the Python Image Library, PIL, is not working for you? Commented Jul 8, 2013 at 15:26
  • 1
    Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. Commented Jul 8, 2013 at 15:27
  • @Doorknob Yikes... I'm afraid that your comment will encourage other users to downvote my question. :( They're going to assume that I haven't made a substantial effort at solving my own problem, and they're going to downvote my question for that reason. :/ So far, I've created an integer array of RGB values and I've asked what I should do in order to create a PNG image from this array. Does this constite a lack of adequate research effort on my part? Commented Jul 8, 2013 at 15:29
  • 1
    @Doorknob I don't see any part of this question that is asking for a library recommendation. An appropriate answer is likely to contain one, but that doesn't make this a bad question. Commented Jul 8, 2013 at 15:36
  • 1
    @MartijnPieters, since PIL isn't part of the standard Python distribution it's unreasonable to expect everyone to have heard of it. You should leave an answer. Commented Jul 8, 2013 at 15:38

2 Answers 2

7

You can use the Python Imaging Library to convert RGB datapoints to most standard formats.

from PIL import Image

newimage = Image.new('RGB', (len(rgbArray[0]), len(rgbArray)))  # type, size
newimage.putdata([tuple(p) for row in rgbArray for p in row])
newimage.save("filename.png")  # takes type from filename extension

which produces: PIL output

The .save() method can also take a format argument, PNG would fix the output to PNG.

(I recommend you install the Pillow fork as it is more actively maintained and adds proper packaging and Python 3 support).

Sign up to request clarification or add additional context in comments.

Comments

1

The Python Imaging Library (PIL) is handy for most imaging needs in Python. The Image module contains a fromstring function and a save function that should do exactly what you're looking for.

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.