Search code examples
pythonnumpyimage-processingscipypython-imageio

How to remove normalization from scipy imsave function


I have a simple task to accomplish, but the current save functions I am finding are not helping me at all. What I have to do is simply convert a gray-scale image to another one which has intensities belonging to a smaller interval (specifically between 120 and 180).

I implemented the conversion (like a changing different temperature scales), but when I save the image, scipy.misc.imsave normalizes it. The conversion is correct because I created histograms to display the intensities before the saving, and they are all lying between the specified range.

I've tried other tools like:

imageio.imwrite(path, img)
numpy.save(path, img)
scipy.misc.toimage(img, cmin=120, cmax=180, mode='L').save(path)

I confess this last one I don't understand the parameters quite well (I have a guess), and the documentation didn't help. Could anybody help me with this issue?

EDIT: I am posting the code

def ex3():
    I = misc.imread(imgname)
    N =  numpy.multiply(I , float(12.0/51.0))
    N = numpy.add(N, 120)
    NEG = I
    NEG = numpy.add(NEG, -255)
    NEG = numpy.absolute(NEG)
    misc.imsave(path, N)
    misc.imsave(os.getcwd()+"/a0/results/"+file.replace(path, NEG)

The image was perfectly rendered when I switched to OpenCV. But I would like to stick with Scipy if possible.


Solution

  • As answered by Warren Weckesser on this question, there is no normalization of the values, but the functions re scale the values according to the data type. The solution for this issue is to set datatype of the array image to uint8 as it can be seen bellow:

    misc.imsave(path, img.astype(numpy.uint8))