def imshow(data, title=None, vmin=0, vmax=None, cmap=None, bitspersample=None, photometric='rgb', interpolation='nearest', dpi=96, figure=None, subplot=111, maxdim=8192, **kwargs):
 isrgb = (photometric in ('rgb', 'palette'))
 data = numpy.atleast_2d(data.squeeze())
 dims = data.ndim
 if (dims < 2):
  raise ValueError('not an image')
 elif (dims == 2):
  dims = 0
  isrgb = False
 else:
  if (isrgb and (data.shape[(-3)] in (3, 4))):
   data = numpy.swapaxes(data, (-3), (-2))
   data = numpy.swapaxes(data, (-2), (-1))
  elif ((not isrgb) and ((data.shape[(-1)] < (data.shape[(-2)] // 8)) and (data.shape[(-1)] < (data.shape[(-3)] // 8)) and (data.shape[(-1)] < 5))):
   data = numpy.swapaxes(data, (-3), (-1))
   data = numpy.swapaxes(data, (-2), (-1))
  isrgb = (isrgb and (data.shape[(-1)] in (3, 4)))
  dims -= (3 if isrgb else 2)
 if isrgb:
  data = data[..., :maxdim, :maxdim, :maxdim]
 else:
  data = data[..., :maxdim, :maxdim]
 if ((photometric == 'palette') and isrgb):
  datamax = data.max()
  if (datamax > 255):
   data >>= 8
  data = data.astype('B')
 elif (data.dtype.kind in 'ui'):
  if ((not (isrgb and (data.dtype.itemsize <= 1))) or (bitspersample is None)):
   try:
    bitspersample = int(math.ceil(math.log(data.max(), 2)))
   except Exception:
    bitspersample = (data.dtype.itemsize * 8)
  elif (not isinstance(bitspersample, int)):
   bitspersample = (data.dtype.itemsize * 8)
  datamax = (2 ** bitspersample)
  if isrgb:
   if (bitspersample < 8):
    data <<= (8 - bitspersample)
   elif (bitspersample > 8):
    data >>= (bitspersample - 8)
   data = data.astype('B')
 elif (data.dtype.kind == 'f'):
  datamax = data.max()
  if (isrgb and (datamax > 1.0)):
   if (data.dtype.char == 'd'):
    data = data.astype('f')
   data /= datamax
 elif (data.dtype.kind == 'b'):
  datamax = 1
 elif (data.dtype.kind == 'c'):
  raise NotImplementedError('complex type')
 if (not isrgb):
  if (vmax is None):
   vmax = datamax
  if (vmin is None):
   if (data.dtype.kind == 'i'):
    dtmin = numpy.iinfo(data.dtype).min
    vmin = numpy.min(data)
    if (vmin == dtmin):
     vmin = numpy.min((data > dtmin))
   if (data.dtype.kind == 'f'):
    dtmin = numpy.finfo(data.dtype).min
    vmin = numpy.min(data)
    if (vmin == dtmin):
     vmin = numpy.min((data > dtmin))
   else:
    vmin = 0
 pyplot = sys.modules['matplotlib.pyplot']
 if (figure is None):
  pyplot.rc('font', family='sans-serif', weight='normal', size=8)
  figure = pyplot.figure(dpi=dpi, figsize=(10.3, 6.3), frameon=True, facecolor='1.0', edgecolor='w')
  try:
   figure.canvas.manager.window.title(title)
  except Exception:
   pass
  pyplot.subplots_adjust(bottom=(0.03 * (dims + 2)), top=0.9, left=0.1, right=0.95, hspace=0.05, wspace=0.0)
 subplot = pyplot.subplot(subplot)
 if title:
  try:
   title = unicode(title, 'Windows-1252')
  except TypeError:
   pass
  pyplot.title(title, size=11)
 if (cmap is None):
  if ((data.dtype.kind in 'ubf') or (vmin == 0)):
   cmap = 'cubehelix'
  else:
   cmap = 'coolwarm'
  if (photometric == 'miniswhite'):
   cmap += '_r'
 image = pyplot.imshow(data[((0,) * dims)].squeeze(), vmin=vmin, vmax=vmax, cmap=cmap, interpolation=interpolation, **kwargs)
 if (not isrgb):
  pyplot.colorbar()
 def format_coord(x, y):
  x = int((x + 0.5))
  y = int((y + 0.5))
  try:
   if dims:
    return ('%s @ %s [%4i, %4i]' % (cur_ax_dat[1][(y, x)], current, x, y))
   else:
    return ('%s @ [%4i, %4i]' % (data[(y, x)], x, y))
  except IndexError:
   return ''
 pyplot.gca().format_coord = format_coord
 if dims:
  current = list(((0,) * dims))
  cur_ax_dat = [0, data[tuple(current)].squeeze()]
  sliders = [pyplot.Slider(pyplot.axes([0.125, (0.03 * (axis + 1)), 0.725, 0.025]), ('Dimension %i' % axis), 0, (data.shape[axis] - 1), 0, facecolor='0.5', valfmt=('%%.0f [%i]' % data.shape[axis])) for axis in range(dims)]
  for slider in sliders:
   slider.drawon = False
  def set_image(current, sliders=sliders, data=data):
   cur_ax_dat[1] = data[tuple(current)].squeeze()
   image.set_data(cur_ax_dat[1])
   for (ctrl, index) in zip(sliders, current):
    ctrl.eventson = False
    ctrl.set_val(index)
    ctrl.eventson = True
   figure.canvas.draw()
  def on_changed(index, axis, data=data, current=current):
   index = int(round(index))
   cur_ax_dat[0] = axis
   if (index == current[axis]):
    return
   if (index >= data.shape[axis]):
    index = 0
   elif (index < 0):
    index = (data.shape[axis] - 1)
   current[axis] = index
   set_image(current)
  def on_keypressed(event, data=data, current=current):
   key = event.key
   axis = cur_ax_dat[0]
   if (str(key) in '0123456789'):
    on_changed(key, axis)
   elif (key == 'right'):
    on_changed((current[axis] + 1), axis)
   elif (key == 'left'):
    on_changed((current[axis] - 1), axis)
   elif (key == 'up'):
    cur_ax_dat[0] = (0 if (axis == (len(data.shape) - 1)) else (axis + 1))
   elif (key == 'down'):
    cur_ax_dat[0] = ((len(data.shape) - 1) if (axis == 0) else (axis - 1))
   elif (key == 'end'):
    on_changed((data.shape[axis] - 1), axis)
   elif (key == 'home'):
    on_changed(0, axis)
  figure.canvas.mpl_connect('key_press_event', on_keypressed)
  for (axis, ctrl) in enumerate(sliders):
   ctrl.on_changed((lambda k, a=axis: on_changed(k, a)))
 return (figure, subplot, image)

def imshow ( data , title = None , vmin = 0 , vmax = None , cmap = None , bitspersample = None , photometric = 'rgb' , interpolation = 'nearest' , dpi = 96 , figure = None , subplot = 111 , maxdim = 8192 , ** kwargs ) : isrgb = ( photometric in ( 'rgb' , 'palette' ) ) data = numpy . atleast_2d ( data . squeeze ( ) ) dims = data . ndim if ( dims < 2 ) : raise ValueError ( 'not an image' ) elif ( dims == 2 ) : dims = 0 isrgb = False else : if ( isrgb and ( data . shape [ ( - 3 ) ] in ( 3 , 4 ) ) ) : data = numpy . swapaxes ( data , ( - 3 ) , ( - 2 ) ) data = numpy . swapaxes ( data , ( - 2 ) , ( - 1 ) ) elif ( ( not isrgb ) and ( ( data . shape [ ( - 1 ) ] < ( data . shape [ ( - 2 ) ] // 8 ) ) and ( data . shape [ ( - 1 ) ] < ( data . shape [ ( - 3 ) ] // 8 ) ) and ( data . shape [ ( - 1 ) ] < 5 ) ) ) : data = numpy . swapaxes ( data , ( - 3 ) , ( - 1 ) ) data = numpy . swapaxes ( data , ( - 2 ) , ( - 1 ) ) isrgb = ( isrgb and ( data . shape [ ( - 1 ) ] in ( 3 , 4 ) ) ) dims -= ( 3 if isrgb else 2 ) if isrgb : data = data [ ... , : maxdim , : maxdim , : maxdim ] else : data = data [ ... , : maxdim , : maxdim ] if ( ( photometric == 'palette' ) and isrgb ) : datamax = data . max ( ) if ( datamax > 255 ) : data >>= 8 data = data . astype ( 'B' ) elif ( data . dtype . kind in 'ui' ) : if ( ( not ( isrgb and ( data . dtype . itemsize <= 1 ) ) ) or ( bitspersample is None ) ) : try : bitspersample = int ( math . ceil ( math . log ( data . max ( ) , 2 ) ) ) except Exception : bitspersample = ( data . dtype . itemsize * 8 ) elif ( not isinstance ( bitspersample , int ) ) : bitspersample = ( data . dtype . itemsize * 8 ) datamax = ( 2 ** bitspersample ) if isrgb : if ( bitspersample < 8 ) : data <<= ( 8 - bitspersample ) elif ( bitspersample > 8 ) : data >>= ( bitspersample - 8 ) data = data . astype ( 'B' ) elif ( data . dtype . kind == 'f' ) : datamax = data . max ( ) if ( isrgb and ( datamax > 1.0 ) ) : if ( data . dtype . char == 'd' ) : data = data . astype ( 'f' ) data /= datamax elif ( data . dtype . kind == 'b' ) : datamax = 1 elif ( data . dtype . kind == 'c' ) : raise NotImplementedError ( 'complex type' ) if ( not isrgb ) : if ( vmax is None ) : vmax = datamax if ( vmin is None ) : if ( data . dtype . kind == 'i' ) : dtmin = numpy . iinfo ( data . dtype ) . min vmin = numpy . min ( data ) if ( vmin == dtmin ) : vmin = numpy . min ( ( data > dtmin ) ) if ( data . dtype . kind == 'f' ) : dtmin = numpy . finfo ( data . dtype ) . min vmin = numpy . min ( data ) if ( vmin == dtmin ) : vmin = numpy . min ( ( data > dtmin ) ) else : vmin = 0 pyplot = sys . modules [ 'matplotlib.pyplot' ] if ( figure is None ) : pyplot . rc ( 'font' , family = 'sans-serif' , weight = 'normal' , size = 8 ) figure = pyplot . figure ( dpi = dpi , figsize = ( 10.3 , 6.3 ) , frameon = True , facecolor = '1.0' , edgecolor = 'w' ) try : figure . canvas . manager . window . title ( title ) except Exception : pass pyplot . subplots_adjust ( bottom = ( 0.03 * ( dims + 2 ) ) , top = 0.9 , left = 0.1 , right = 0.95 , hspace = 0.05 , wspace = 0.0 ) subplot = pyplot . subplot ( subplot ) if title : try : title = unicode ( title , 'Windows-1252' ) except TypeError : pass pyplot . title ( title , size = 11 ) if ( cmap is None ) : if ( ( data . dtype . kind in 'ubf' ) or ( vmin == 0 ) ) : cmap = 'cubehelix' else : cmap = 'coolwarm' if ( photometric == 'miniswhite' ) : cmap += '_r' image = pyplot . imshow ( data [ ( ( 0 , ) * dims ) ] . squeeze ( ) , vmin = vmin , vmax = vmax , cmap = cmap , interpolation = interpolation , ** kwargs ) if ( not isrgb ) : pyplot . colorbar ( ) def format_coord ( x , y ) : x = int ( ( x + 0.5 ) ) y = int ( ( y + 0.5 ) ) try : if dims : return ( '%s @ %s [%4i, %4i]' % ( cur_ax_dat [ 1 ] [ ( y , x ) ] , current , x , y ) ) else : return ( '%s @ [%4i, %4i]' % ( data [ ( y , x ) ] , x , y ) ) except IndexError : return '' pyplot . gca ( ) . format_coord = format_coord if dims : current = list ( ( ( 0 , ) * dims ) ) cur_ax_dat = [ 0 , data [ tuple ( current ) ] . squeeze ( ) ] sliders = [ pyplot . Slider ( pyplot . axes ( [ 0.125 , ( 0.03 * ( axis + 1 ) ) , 0.725 , 0.025 ] ) , ( 'Dimension %i' % axis ) , 0 , ( data . shape [ axis ] - 1 ) , 0 , facecolor = '0.5' , valfmt = ( '%%.0f [%i]' % data . shape [ axis ] ) ) for axis in range ( dims ) ] for slider in sliders : slider . drawon = False def set_image ( current , sliders = sliders , data = data ) : cur_ax_dat [ 1 ] = data [ tuple ( current ) ] . squeeze ( ) image . set_data ( cur_ax_dat [ 1 ] ) for ( ctrl , index ) in zip ( sliders , current ) : ctrl . eventson = False ctrl . set_val ( index ) ctrl . eventson = True figure . canvas . draw ( ) def on_changed ( index , axis , data = data , current = current ) : index = int ( round ( index ) ) cur_ax_dat [ 0 ] = axis if ( index == current [ axis ] ) : return if ( index >= data . shape [ axis ] ) : index = 0 elif ( index < 0 ) : index = ( data . shape [ axis ] - 1 ) current [ axis ] = index set_image ( current ) def on_keypressed ( event , data = data , current = current ) : key = event . key axis = cur_ax_dat [ 0 ] if ( str ( key ) in '0123456789' ) : on_changed ( key , axis ) elif ( key == 'right' ) : on_changed ( ( current [ axis ] + 1 ) , axis ) elif ( key == 'left' ) : on_changed ( ( current [ axis ] - 1 ) , axis ) elif ( key == 'up' ) : cur_ax_dat [ 0 ] = ( 0 if ( axis == ( len ( data . shape ) - 1 ) ) else ( axis + 1 ) ) elif ( key == 'down' ) : cur_ax_dat [ 0 ] = ( ( len ( data . shape ) - 1 ) if ( axis == 0 ) else ( axis - 1 ) ) elif ( key == 'end' ) : on_changed ( ( data . shape [ axis ] - 1 ) , axis ) elif ( key == 'home' ) : on_changed ( 0 , axis ) figure . canvas . mpl_connect ( 'key_press_event' , on_keypressed ) for ( axis , ctrl ) in enumerate ( sliders ) : ctrl . on_changed ( ( lambda k , a = axis : on_changed ( k , a ) ) ) return ( figure , subplot , image )
show the input image and return the current axes .