Search code examples
pythonrotationmedianidl-programming-language

Python equivalent to IDL median() and rot()


I'm working with fits images and trying to translate IDL code to python but I'm having trouble understanding how to translate the IDL median() and rot() functions. I have an array im_darksub that is (ny, nx, n_im) where nx,ny is the shape of each image and n_im is the number of images (working with 12 images total). I trying to translate the IDL code im_median = median(im_darksub,dimension=1) and im_medrot = rot(im_median,85,cubic=-0.5). I thought that I could just translate this to np.median() and spicy.misc.imrotate, but I keep getting an error when I use imrotate:

    Traceback (innermost last):
      File "<console>", line 1, in <module>
      File "/Users/courtneywatson1/Ureka/python/lib/python2.7/site-packages/scipy/misc/pilutil.py", line 345, in imrotate
        im = toimage(arr)
      File "/Users/courtneywatson1/Ureka/python/lib/python2.7/site-packages/scipy/misc/pilutil.py", line 234, in toimage
        raise ValueError("'arr' does not have a suitable array shape for "
    ValueError: 'arr' does not have a suitable array shape for any mode.

Which I'm guessing is because my image array is more than 2 or 3D? Is there a python equivalent for the IDL median() and rot() functions the way I am using them?


Solution

  • This worked for me:

    import numpy as np
    x = np.arange(100)
    x = np.reshape(x, (5, 5, 4))
    im_median = np.median(x, axis=2)
    
    import scipy.misc
    im_rotate = scipy.misc.imrotate(y, 85, 'cubic')
    

    And I assume you saw that imrotate is deprecated:

    /Users/mgalloy/anaconda3/bin/ipython:1: DeprecationWarning: `imrotate` is deprecated!
    `imrotate` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
    Use ``skimage.transform.rotate`` instead.
    

    This seemed to work just fine:

    import skimage.transform
    im_rotate = skimage.transform.rotate(im_median, 85, order=3, preserve_range=True)