pythonimage-processingmahotas

Reading and writing images with Mahotas


I'm trying to write an image with Mahotas and finding it strangely difficult.

img = mahotas.imread('foo.png', True)
mahotas.imsave('bar.png', img)

the error I'm gettings is:

ValueError: mahotas.freeimage: cannot write arrays of given type and shape.

I'm on OS X and used brew to install freeimage.


Solution

  • Author of mahotas here. The error message is not ideal (will fix it), but here's what's happening.

    The greyscale image is a floating point image (i.e., img.dtype == numpy.float64) and you cannot save floating point images as PNGs.

    Convert to numpy.uint8:

    mahotas.imsave('test.png', img.astype(numpy.uint8))
    

    and it will work as expected.