[Numpy-discussion] create a numpy array of images

Francesc Alted faltet@pytables....
Thu Feb 3 05:33:08 CST 2011


A Wednesday 02 February 2011 18:12:47 Christopher Barker escrigué:
> One other option, that I've never tried, is carray, which is an array
> compressed in memory. Depending on your images, perhaps they would
> compress a lot (or not ....):
> 
> https://github.com/FrancescAlted/carray
> http://mail.scipy.org/pipermail/numpy-discussion/2010-August/052378.h
> tml

Nice idea.  In 0.3.1 release I've just implemented preliminary support 
for multidimensional data.  So I was curious on the kind of compression 
that can be achieved on images:

# Preliminaries: load numpy, matplotlib an carray libs
>>> import numpy as np
>>> import matplotlib.image as mpimg
>>> import matplotlib.pyplot as plt
>>> import carray as ca

First I tried the classic Lenna (http://en.wikipedia.org/wiki/Lenna):

>>> img = mpimg.imread('Lenna.png')
>>> cimg = ca.carray(img)
>>> cimg.nbytes/float(cimg.cbytes)
1.2450163377998429

So, just a 25% compression, not too much.  But trying another example 
(http://matplotlib.sourceforge.net/_images/stinkbug.png) gives a 
significantly better ratio:

>>> img2 = mpimg.imread('stinkbug.png')
>>> cimg2 = ca.carray(img2)
>>> cimg2.nbytes/float(cimg2.cbytes)
2.7716869102466184

And finally, the beautiful NumPy container drawing by Stéfan van der 
Walt (slide 31 of his presentation in our latest advanced Python course,
https://portal.g-node.org/python-autumnschool/materials/advanced_numpy):

>>> img3 = mpimg.imread('numpy-container.png')
>>> cimg3 = ca.carray(img3)
>>> cimg3.nbytes/float(cimg3.cbytes)
3.7915321810785132

So, yeah, depending on the images, carray could be a nice way to keep 
them in-memory.  And although, as I said, multidimensional support is 
still preliminary, matplotlib already understands carray beasts:

# plotting image
>>> imshow(cimg3)
<matplotlib.image.AxesImage object at 0x27d2150>

Cheers,

-- 
Francesc Alted


More information about the NumPy-Discussion mailing list