Example #1
0
def make_image(redband,greenband,blueband,rows,cols,enhance):
    if str(redband.dtype) == 'uint8':
        dt = 1
    elif str(redband.dtype) == 'uint16':
        dt = 2
    elif str(redband.dtype) == 'int16':
        dt = 2        
    elif str(redband.dtype) == 'float32':
        dt = 4
    elif str(redband.dtype) == 'float64':
        dt = 6
    else:
        print 'Unrecognized format'
        return   
    redband = redband.tostring()
    greenband = greenband.tostring()
    blueband = blueband.tostring()
    if enhance == 'linear255':
        rng = [0.0,255.0]
    else:
        rng = None
    if dt != 1: 
        redband   = auxil.byte_stretch(redband,dtype=dt,rng=rng)
        greenband = auxil.byte_stretch(greenband,dtype=dt,rng=rng)
        blueband  = auxil.byte_stretch(blueband,dtype=dt,rng=rng)        
    r,g,b = auxil.stretch(redband,greenband,blueband,enhance)  
    X = np.zeros((rows*cols,3),dtype=np.float32)                                                                                                                 
    X[:,0] = np.float32(np.fromstring(r,dtype=np.uint8))
    X[:,1] = np.float32(np.fromstring(g,dtype=np.uint8))
    X[:,2] = np.float32(np.fromstring(b,dtype=np.uint8))
    return np.reshape(X,(rows,cols,3))/255.
Example #2
0
def dispms(filename=None,dims=None,rgb=None,enhance=None):
    gdal.AllRegister()
    if filename == None:        
        filename = auxil.select_infile(title='Choose an image to display') 
    if filename:                   
        inDataset = gdal.Open(filename,GA_ReadOnly)     
        cols =  inDataset.RasterXSize
        rows =  inDataset.RasterYSize    
        bands = inDataset.RasterCount
    else:
        return 
    if dims == None:
        dims = auxil.select_dims([0,0,cols,rows])
    if dims:
        x0,y0,cols,rows = dims
    else:
        return
    if rgb == None:
        rgb = auxil.select_rgb(bands)
    if rgb:
        r,g,b = rgb
    else:
        return
    if enhance == None:
        enhance = auxil.select_enhance('3')
    if enhance == '1':
        enhance = 'linear255'
    elif enhance == '2':
        enhance = 'linear'
    elif enhance == '3':
        enhance = 'linear2pc'
    elif enhance == '4':
        enhance = 'equalization'
    else:
        return    
    redband   = inDataset.GetRasterBand(r).ReadAsArray(x0,y0,cols,rows)
    greenband = inDataset.GetRasterBand(g).ReadAsArray(x0,y0,cols,rows)  
    blueband  = inDataset.GetRasterBand(b).ReadAsArray(x0,y0,cols,rows)
    if str(redband.dtype) == 'uint8':
        dt = 1
    elif str(redband.dtype) == 'uint16':
        dt = 2
    elif str(redband.dtype) == 'int16':
        dt = 2        
    elif str(redband.dtype) == 'float32':
        dt = 4
    elif str(redband.dtype) == 'float64':
        dt = 6
    else:
        print 'Unrecognized format'
        return   
    redband = redband.tostring()
    greenband = greenband.tostring()
    blueband = blueband.tostring()
    if dt != 1: 
        redband   = auxil.byte_stretch(redband,dtype=dt)
        greenband = auxil.byte_stretch(greenband,dtype=dt)
        blueband  = auxil.byte_stretch(blueband,dtype=dt)        
    r,g,b = auxil.stretch(redband,greenband,blueband,enhance)                                                                                                                   
    bip = ''
    for i in range(cols*rows):
        bip += r[i]+g[i]+b[i]
    im = Image.fromstring('RGB', (cols,rows), bip, 'raw', ('RGB',3*cols,1))
    print 'close image to finish' 
    im.show()
    print 'done'