Python:Images

From GISAXS
Jump to: navigation, search

ndimage.imread

ndimage.imread has been deprecated. Here are alternatives.

    #image = scipy.misc.imread(infile) # Deprecated
    image = imageio.imread(infile)

Helpers:

def sm_imresize(image, size, handle_float=False):
    '''Replacement for deprecated scipy.misc.imresize function.'''
    #image = scipy.misc.imresize(image, size) # Deprecated
    
    h, w, c = image.shape
    if isinstance(size, (int, float)):
        hn = int(h*size)
        wn = int(w*size)
    elif len(size)==2:
        hn, wn = size
    else:
        print('Error in sm_imresize.')
    
    if handle_float:
        image = np.copy(image)*255
        image = np.array( Image.fromarray( image.astype(np.uint8) ).resize((wn,hn)) )
        image = image/255
    else:
        image = np.array( Image.fromarray( image.astype(np.uint8) ).resize((wn,hn)) )
        #image = resize(image, output_shape=(hn,wn), preserve_range=True) # Doesn't work
                        
    return image
    
    
def sm_imread(infile):    
    #image = scipy.misc.imread(infile) # Deprecated
    image = imageio.imread(infile)
    if image.ndim==2:
        # Convert single-channel (grayscale) to 3-channel
        image = np.stack((image,)*3, axis=-1)
    #h, w, c = image.shape
    #aspect_ratio = w/h
    
    return image