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]],
]
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • 1
    And the Python Image Library, PIL, is not working for you? – Martijn Pieters Jul 08 '13 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. – tckmn Jul 08 '13 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? – Anderson Green Jul 08 '13 at 15:29
  • @MartijnPieters That might be useful: how can I generate a PNG image from an RGB array using the Python Image library? – Anderson Green Jul 08 '13 at 15:35
  • 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. – Mark Ransom Jul 08 '13 at 15:36
  • @MarkRansom The question was edited. Unfortunately, I can't undo my close vote `:(` – tckmn Jul 08 '13 at 15:36
  • @AndersonGreen: All image data in PIL consists of arrays of pixel data. Only on save is that data converted to a specific image compression format. Read up on the documentation, your task is relatively easy. – Martijn Pieters Jul 08 '13 at 15:37
  • It does show a lack of effort. You are basically asking us to write code for you: *given a list of lists as input, write me code to turn that into a PNG file*. – Martijn Pieters Jul 08 '13 at 15:38
  • 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. – Mark Ransom Jul 08 '13 at 15:38
  • @MartijnPieters I'm relatively inexperienced with Python (and I'm mainly a JavaScript/Java developer), so I'm surprised that everyone expects me to be an expert on this subject. :/ – Anderson Green Jul 08 '13 at 15:40
  • For the record, I would *hugely* recommend the fork, [`Pillow`](https://github.com/python-imaging/Pillow), over `PIL`. The latter is no longer under development, and it's nearly impossible to install on modern systems. `Pillow` is far friendlier. – Henry Keiter Jul 08 '13 at 15:40

2 Answers2

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).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622