Saving a Numpy array as an image
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Question
00:27 Accepted answer (Score 67)
00:47 Answer 2 (Score 415)
01:11 Answer 3 (Score 272)
01:42 Answer 4 (Score 124)
02:17 Thank you
--
Full question
https://stackoverflow.com/questions/9027...
Accepted answer links:
[PyPNG]: https://github.com/drj11/pypng/
[supports]: https://pypng.readthedocs.io/en/latest/e...
Answer 2 links:
[PIL]: http://www.pythonware.com/products/pil/
[here]: http://www.pythonware.com/library/pil/ha...
Answer 4 links:
[image]: https://i.stack.imgur.com/J1pnL.png
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #image #numpy
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow
--
Chapters
00:00 Question
00:27 Accepted answer (Score 67)
00:47 Answer 2 (Score 415)
01:11 Answer 3 (Score 272)
01:42 Answer 4 (Score 124)
02:17 Thank you
--
Full question
https://stackoverflow.com/questions/9027...
Accepted answer links:
[PyPNG]: https://github.com/drj11/pypng/
[supports]: https://pypng.readthedocs.io/en/latest/e...
Answer 2 links:
[PIL]: http://www.pythonware.com/products/pil/
[here]: http://www.pythonware.com/library/pil/ha...
Answer 4 links:
[image]: https://i.stack.imgur.com/J1pnL.png
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #image #numpy
#avk47
ANSWER 1
Score 462
Using PIL, save a NumPy array arr by doing:
from PIL import Image
im = Image.fromarray(arr)
im.save("your_file.jpeg")
See the docs for available data formats, including JPEG, PNG, and so on.
ANSWER 2
Score 273
This uses PIL, but maybe some might find it useful:
import scipy.misc
scipy.misc.imsave('outfile.jpg', image_array)
EDIT: The current scipy version started to normalize all images so that min(data) become black and max(data) become white. This is unwanted if the data should be exact grey levels or exact RGB channels. The solution:
import scipy.misc
scipy.misc.toimage(image_array, cmin=0.0, cmax=...).save('outfile.jpg')
ANSWER 3
Score 136
With matplotlib:
import matplotlib.image
matplotlib.image.imsave('name.png', array)
Works with matplotlib 1.3.1, I don't know about lower version. From the docstring:
Arguments:
*fname*:
A string containing a path to a filename, or a Python file-like object.
If *format* is *None* and *fname* is a string, the output
format is deduced from the extension of the filename.
*arr*:
An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
ANSWER 4
Score 85
Pure Python (2 & 3), a snippet without 3rd party dependencies.
This function writes compressed, true-color (4 bytes per pixel) RGBA PNG's.
def write_png(buf, width, height):
""" buf: must be bytes or a bytearray in Python3.x,
a regular string in Python2.x.
"""
import zlib, struct
# reverse the vertical line order and add null bytes at the start
width_byte_4 = width * 4
raw_data = b''.join(
b'\x00' + buf[span:span + width_byte_4]
for span in range((height - 1) * width_byte_4, -1, - width_byte_4)
)
def png_pack(png_tag, data):
chunk_head = png_tag + data
return (struct.pack("!I", len(data)) +
chunk_head +
struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head)))
return b''.join([
b'\x89PNG\r\n\x1a\n',
png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)),
png_pack(b'IDAT', zlib.compress(raw_data, 9)),
png_pack(b'IEND', b'')])
... The data should be written directly to a file opened as binary, as in:
data = write_png(buf, 64, 64)
with open("my_image.png", 'wb') as fh:
fh.write(data)
- Original source
- See also: Rust Port from this question.
- Example usage thanks to @Evgeni Sergeev: https://stackoverflow.com/a/21034111/432509
