<s> """<STR_LIT>""" <EOL> import logging <EOL> import numpy as np <EOL> plt = None <EOL> axes = None <EOL> from theano . compat . six . moves import xrange <EOL> from theano . compat . six import string_types <EOL> import warnings <EOL> try : <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . axes <EOL> except ( RuntimeError , ImportError , TypeError ) as matplotlib_exception : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" + str ( matplotlib_exception ) ) <EOL> import os <EOL> try : <EOL> from PIL import Image <EOL> except ImportError : <EOL> Image = None <EOL> from pylearn2 . utils import string_utils as string <EOL> from pylearn2 . utils . exc import reraise_as <EOL> from tempfile import mkstemp <EOL> from multiprocessing import Process <EOL> import subprocess <EOL> logger = logging . getLogger ( __name__ ) <EOL> def ensure_Image ( ) : <EOL> """<STR_LIT>""" <EOL> global Image <EOL> if Image is None : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def imview ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in kwargs : <EOL> f = plt . figure ( ) <EOL> else : <EOL> f = kwargs [ '<STR_LIT>' ] <EOL> new_ax = matplotlib . axes . Axes ( f , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> xticks = [ ] , <EOL> yticks = [ ] , <EOL> frame_on = False ) <EOL> f . delaxes ( f . gca ( ) ) <EOL> f . add_axes ( new_ax ) <EOL> if len ( args ) < <NUM_LIT:5> and '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> plt . imshow ( * args , ** kwargs ) <EOL> def imview_async ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def fork_image_viewer ( ) : <EOL> f = plt . figure ( ) <EOL> kwargs [ '<STR_LIT>' ] = f <EOL> imview ( * args , ** kwargs ) <EOL> if '<STR_LIT>' in kwargs : <EOL> f . set_window_title ( kwargs [ '<STR_LIT>' ] ) <EOL> plt . show ( ) <EOL> p = Process ( None , fork_image_viewer ) <EOL> p . start ( ) <EOL> return p <EOL> def show ( image ) : <EOL> """<STR_LIT>""" <EOL> viewer_command = string . preprocess ( '<STR_LIT>' ) <EOL> if viewer_command == '<STR_LIT>' : <EOL> return imview ( image ) <EOL> if hasattr ( image , '<STR_LIT>' ) : <EOL> if len ( image . shape ) < <NUM_LIT:2> or len ( image . shape ) > <NUM_LIT:3> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' + str ( image . shape ) ) <EOL> max_height = <NUM_LIT> <EOL> max_width = <NUM_LIT> <EOL> if image . shape [ <NUM_LIT:0> ] > max_height : <EOL> image = image [ <NUM_LIT:0> : max_height , : , : ] <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if image . shape [ <NUM_LIT:0> ] > max_width : <EOL> image = image [ : , <NUM_LIT:0> : max_width , : ] <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if image . dtype == '<STR_LIT>' : <EOL> image = np . cast [ '<STR_LIT>' ] ( image ) <EOL> elif str ( image . dtype ) . startswith ( '<STR_LIT:float>' ) : <EOL> image = image * <NUM_LIT> <EOL> image = np . cast [ '<STR_LIT>' ] ( image ) <EOL> if len ( image . shape ) == <NUM_LIT:3> and image . shape [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> image = image [ : , : , <NUM_LIT:0> ] <EOL> try : <EOL> ensure_Image ( ) <EOL> image = Image . fromarray ( image ) <EOL> except TypeError : <EOL> reraise_as ( TypeError ( "<STR_LIT>" + <EOL> str ( image . shape ) + "<STR_LIT>" + <EOL> str ( image . dtype ) ) ) <EOL> fd , name = mkstemp ( suffix = '<STR_LIT>' ) <EOL> os . close ( fd ) <EOL> image . save ( name ) <EOL> if os . name == '<STR_LIT>' : <EOL> subprocess . Popen ( viewer_command + '<STR_LIT:U+0020>' + name + '<STR_LIT>' + name , <EOL> shell = True ) <EOL> else : <EOL> subprocess . Popen ( viewer_command + '<STR_LIT:U+0020>' + name + '<STR_LIT>' + name , <EOL> shell = True ) <EOL> def pil_from_ndarray ( ndarray ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if ndarray . dtype == '<STR_LIT>' or ndarray . dtype == '<STR_LIT>' : <EOL> assert ndarray . min ( ) >= <NUM_LIT:0.0> <EOL> assert ndarray . max ( ) <= <NUM_LIT:1.0> <EOL> ndarray = np . cast [ '<STR_LIT>' ] ( ndarray * <NUM_LIT:255> ) <EOL> if len ( ndarray . shape ) == <NUM_LIT:3> and ndarray . shape [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> ndarray = ndarray [ : , : , <NUM_LIT:0> ] <EOL> ensure_Image ( ) <EOL> rval = Image . fromarray ( ndarray ) <EOL> return rval <EOL> except Exception as e : <EOL> logger . exception ( '<STR_LIT>' ) <EOL> logger . exception ( e ) <EOL> logger . exception ( '<STR_LIT>' . format ( ndarray . dtype ) ) <EOL> logger . exception ( '<STR_LIT>' . format ( ndarray . shape ) ) <EOL> raise <EOL> assert False <EOL> def ndarray_from_pil ( pil , dtype = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> rval = np . asarray ( pil ) <EOL> if dtype != rval . dtype : <EOL> rval = np . cast [ dtype ] ( rval ) <EOL> if str ( dtype ) . startswith ( '<STR_LIT:float>' ) : <EOL> rval /= <NUM_LIT> <EOL> if len ( rval . shape ) == <NUM_LIT:2> : <EOL> rval = rval . reshape ( rval . shape [ <NUM_LIT:0> ] , rval . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> return rval <EOL> def rescale ( image , shape ) : <EOL> """<STR_LIT>""" <EOL> assert len ( image . shape ) == <NUM_LIT:3> <EOL> assert len ( shape ) == <NUM_LIT:2> <EOL> i = pil_from_ndarray ( image ) <EOL> ensure_Image ( ) <EOL> i . thumbnail ( [ shape [ <NUM_LIT:1> ] , shape [ <NUM_LIT:0> ] ] , Image . ANTIALIAS ) <EOL> rval = ndarray_from_pil ( i , dtype = image . dtype ) <EOL> return rval <EOL> resize = rescale <EOL> def fit_inside ( image , shape ) : <EOL> """<STR_LIT>""" <EOL> assert len ( image . shape ) == <NUM_LIT:3> <EOL> assert len ( shape ) == <NUM_LIT:2> <EOL> if image . shape [ <NUM_LIT:0> ] <= shape [ <NUM_LIT:0> ] and image . shape [ <NUM_LIT:1> ] <= shape [ <NUM_LIT:1> ] : <EOL> return image . copy ( ) <EOL> row_ratio = float ( image . shape [ <NUM_LIT:0> ] ) / float ( shape [ <NUM_LIT:0> ] ) <EOL> col_ratio = float ( image . shape [ <NUM_LIT:1> ] ) / float ( shape [ <NUM_LIT:1> ] ) <EOL> if row_ratio > col_ratio : <EOL> target_shape = [ shape [ <NUM_LIT:0> ] , min ( image . shape [ <NUM_LIT:1> ] / row_ratio , shape [ <NUM_LIT:1> ] ) ] <EOL> else : <EOL> target_shape = [ min ( image . shape [ <NUM_LIT:0> ] / col_ratio , shape [ <NUM_LIT:0> ] ) , shape [ <NUM_LIT:1> ] ] <EOL> assert target_shape [ <NUM_LIT:0> ] <= shape [ <NUM_LIT:0> ] <EOL> assert target_shape [ <NUM_LIT:1> ] <= shape [ <NUM_LIT:1> ] <EOL> assert target_shape [ <NUM_LIT:0> ] == shape [ <NUM_LIT:0> ] or target_shape [ <NUM_LIT:1> ] == shape [ <NUM_LIT:1> ] <EOL> rval = rescale ( image , target_shape ) <EOL> return rval <EOL> def letterbox ( image , shape ) : <EOL> """<STR_LIT>""" <EOL> assert len ( image . shape ) == <NUM_LIT:3> <EOL> assert len ( shape ) == <NUM_LIT:2> <EOL> assert image . shape [ <NUM_LIT:0> ] <= shape [ <NUM_LIT:0> ] <EOL> assert image . shape [ <NUM_LIT:1> ] <= shape [ <NUM_LIT:1> ] <EOL> if image . shape [ <NUM_LIT:0> ] == shape [ <NUM_LIT:0> ] and image . shape [ <NUM_LIT:1> ] == shape [ <NUM_LIT:1> ] : <EOL> return image . copy ( ) <EOL> rval = np . zeros ( ( shape [ <NUM_LIT:0> ] , shape [ <NUM_LIT:1> ] , image . shape [ <NUM_LIT:2> ] ) , dtype = image . dtype ) <EOL> rstart = ( shape [ <NUM_LIT:0> ] - image . shape [ <NUM_LIT:0> ] ) / <NUM_LIT:2> <EOL> cstart = ( shape [ <NUM_LIT:1> ] - image . shape [ <NUM_LIT:1> ] ) / <NUM_LIT:2> <EOL> rend = rstart + image . shape [ <NUM_LIT:0> ] <EOL> cend = cstart + image . shape [ <NUM_LIT:1> ] <EOL> rval [ rstart : rend , cstart : cend ] = image <EOL> return rval <EOL> def make_letterboxed_thumbnail ( image , shape ) : <EOL> """<STR_LIT>""" <EOL> assert len ( image . shape ) == <NUM_LIT:3> <EOL> assert len ( shape ) == <NUM_LIT:2> <EOL> shrunk = fit_inside ( image , shape ) <EOL> letterboxed = letterbox ( shrunk , shape ) <EOL> return letterboxed <EOL> def load ( filepath , rescale_image = True , dtype = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( filepath , string_types ) <EOL> if not rescale_image and dtype == '<STR_LIT>' : <EOL> ensure_Image ( ) <EOL> rval = np . asarray ( Image . open ( filepath ) ) <EOL> assert rval . dtype == '<STR_LIT>' <EOL> return rval <EOL> s = <NUM_LIT:1.0> <EOL> if rescale_image : <EOL> s = <NUM_LIT> <EOL> try : <EOL> ensure_Image ( ) <EOL> rval = Image . open ( filepath ) <EOL> except Exception : <EOL> reraise_as ( Exception ( "<STR_LIT>" + filepath ) ) <EOL> numpy_rval = np . array ( rval ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if numpy_rval . ndim not in [ <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> logger . error ( dir ( rval ) ) <EOL> logger . error ( rval ) <EOL> logger . error ( rval . size ) <EOL> rval . show ( ) <EOL> raise AssertionError ( msg % numpy_rval . ndim ) <EOL> rval = numpy_rval <EOL> rval = np . cast [ dtype ] ( rval ) / s <EOL> if rval . ndim == <NUM_LIT:2> : <EOL> rval = rval . reshape ( rval . shape [ <NUM_LIT:0> ] , rval . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> if rval . ndim != <NUM_LIT:3> : <EOL> raise AssertionError ( "<STR_LIT>" + <EOL> filepath + '<STR_LIT>' + <EOL> str ( rval . shape ) + <EOL> "<STR_LIT>" ) <EOL> return rval <EOL> def save ( filepath , ndarray ) : <EOL> """<STR_LIT>""" <EOL> pil_from_ndarray ( ndarray ) . save ( filepath ) <EOL> def scale_to_unit_interval ( ndar , eps = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> ndar = ndar . copy ( ) <EOL> ndar -= ndar . min ( ) <EOL> ndar *= <NUM_LIT:1.0> / ( ndar . max ( ) + eps ) <EOL> return ndar <EOL> def tile_raster_images ( X , img_shape , tile_shape , tile_spacing = ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> scale_rows_to_unit_interval = True , <EOL> output_pixel_vals = True ) : <EOL> """<STR_LIT>""" <EOL> assert len ( img_shape ) == <NUM_LIT:2> <EOL> assert len ( tile_shape ) == <NUM_LIT:2> <EOL> assert len ( tile_spacing ) == <NUM_LIT:2> <EOL> out_shape = [ ( ishp + tsp ) * tshp - tsp for ishp , tshp , tsp <EOL> in zip ( img_shape , tile_shape , tile_spacing ) ] <EOL> if isinstance ( X , tuple ) : <EOL> assert len ( X ) == <NUM_LIT:4> <EOL> if output_pixel_vals : <EOL> out_array = np . zeros ( ( out_shape [ <NUM_LIT:0> ] , out_shape [ <NUM_LIT:1> ] , <NUM_LIT:4> ) , <EOL> dtype = '<STR_LIT>' ) <EOL> else : <EOL> out_array = np . zeros ( ( out_shape [ <NUM_LIT:0> ] , out_shape [ <NUM_LIT:1> ] , <NUM_LIT:4> ) , <EOL> dtype = X . dtype ) <EOL> if output_pixel_vals : <EOL> channel_defaults = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ] <EOL> else : <EOL> channel_defaults = [ <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> ] <EOL> for i in xrange ( <NUM_LIT:4> ) : <EOL> if X [ i ] is None : <EOL> dt = out_array . dtype <EOL> if output_pixel_vals : <EOL> dt = '<STR_LIT>' <EOL> out_array [ : , : , i ] = np . zeros ( out_shape , dtype = dt ) + channel_defaults [ i ] <EOL> else : <EOL> out_array [ : , : , i ] = tile_raster_images ( <EOL> X [ i ] , img_shape , tile_shape , tile_spacing , <EOL> scale_rows_to_unit_interval , output_pixel_vals ) <EOL> return out_array <EOL> else : <EOL> H , W = img_shape <EOL> Hs , Ws = tile_spacing <EOL> dt = X . dtype <EOL> if output_pixel_vals : <EOL> dt = '<STR_LIT>' <EOL> out_array = np . zeros ( out_shape , dtype = dt ) <EOL> for tile_row in xrange ( tile_shape [ <NUM_LIT:0> ] ) : <EOL> for tile_col in xrange ( tile_shape [ <NUM_LIT:1> ] ) : <EOL> if tile_row * tile_shape [ <NUM_LIT:1> ] + tile_col < X . shape [ <NUM_LIT:0> ] : <EOL> this_x = X [ tile_row * tile_shape [ <NUM_LIT:1> ] + tile_col ] <EOL> if scale_rows_to_unit_interval : <EOL> this_img = scale_to_unit_interval ( <EOL> this_x . reshape ( img_shape ) ) <EOL> else : <EOL> this_img = this_x . reshape ( img_shape ) <EOL> c = <NUM_LIT:1> <EOL> if output_pixel_vals : <EOL> c = <NUM_LIT:255> <EOL> out_array [ <EOL> tile_row * ( H + Hs ) : tile_row * ( H + Hs ) + H , <EOL> tile_col * ( W + Ws ) : tile_col * ( W + Ws ) + W <EOL> ] = this_img * c <EOL> return out_array <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> black = np . zeros ( ( <NUM_LIT:50> , <NUM_LIT:50> , <NUM_LIT:3> ) , dtype = '<STR_LIT>' ) <EOL> red = black . copy ( ) <EOL> red [ : , : , <NUM_LIT:0> ] = <NUM_LIT:255> <EOL> green = black . copy ( ) <EOL> green [ : , : , <NUM_LIT:1> ] = <NUM_LIT:255> <EOL> show ( black ) <EOL> show ( green ) <EOL> show ( red ) </s>
