<s> import numpy <EOL> from io import BytesIO <EOL> import cv2 <EOL> from . import CvHelp <EOL> from ginga import ImageView <EOL> from ginga . cvw . CanvasRenderCv import CanvasRenderer <EOL> try : <EOL> import PIL . Image as PILimage <EOL> have_PIL = True <EOL> except ImportError : <EOL> have_PIL = False <EOL> class ImageViewCvError ( ImageView . ImageViewError ) : <EOL> pass <EOL> class ImageViewCv ( ImageView . ImageViewBase ) : <EOL> def __init__ ( self , logger = None , rgbmap = None , settings = None ) : <EOL> ImageView . ImageViewBase . __init__ ( self , logger = logger , <EOL> rgbmap = rgbmap , <EOL> settings = settings ) <EOL> self . surface = None <EOL> self . _rgb_order = '<STR_LIT>' <EOL> self . renderer = CanvasRenderer ( self ) <EOL> self . message = None <EOL> self . cursor = { } <EOL> self . t_ . setDefaults ( show_pan_position = False , <EOL> onscreen_ff = '<STR_LIT>' ) <EOL> def get_surface ( self ) : <EOL> return self . surface <EOL> def render_image ( self , rgbobj , dst_x , dst_y ) : <EOL> """<STR_LIT>""" <EOL> if self . surface is None : <EOL> return <EOL> canvas = self . surface <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> rgb_arr = self . getwin_array ( order = self . _rgb_order ) <EOL> canvas [ : , : , : ] = rgb_arr <EOL> cr = CvHelp . CvContext ( canvas ) <EOL> if self . t_ [ '<STR_LIT>' ] : <EOL> ctr_x , ctr_y = self . get_center ( ) <EOL> pen = cr . get_pen ( '<STR_LIT>' ) <EOL> cr . line ( ( ctr_x - <NUM_LIT:10> , ctr_y ) , ( ctr_x + <NUM_LIT:10> , ctr_y ) , pen ) <EOL> cr . line ( ( ctr_x , ctr_y - <NUM_LIT:10> ) , ( ctr_x , ctr_y + <NUM_LIT:10> ) , pen ) <EOL> if self . message : <EOL> font = cr . get_font ( self . t_ [ '<STR_LIT>' ] , <NUM_LIT> , self . img_fg , <EOL> linewidth = <NUM_LIT:2> ) <EOL> wd , ht = cr . text_extents ( self . message , font ) <EOL> imgwin_wd , imgwin_ht = self . get_window_size ( ) <EOL> y = ( ( imgwin_ht // <NUM_LIT:3> ) * <NUM_LIT:2> ) - ( ht // <NUM_LIT:2> ) <EOL> x = ( imgwin_wd // <NUM_LIT:2> ) - ( wd // <NUM_LIT:2> ) <EOL> cr . text ( ( x , y ) , self . message , font ) <EOL> def configure_surface ( self , width , height ) : <EOL> depth = len ( self . _rgb_order ) <EOL> self . surface = numpy . zeros ( ( height , width , depth ) , numpy . uint8 ) <EOL> self . configure ( width , height ) <EOL> def get_image_as_array ( self ) : <EOL> if self . surface is None : <EOL> raise ImageViewCvError ( "<STR_LIT>" ) <EOL> arr8 = self . get_surface ( ) <EOL> return numpy . copy ( arr8 ) <EOL> def get_image_as_buffer ( self , output = None ) : <EOL> obuf = output <EOL> if obuf is None : <EOL> obuf = BytesIO ( ) <EOL> arr8 = self . get_image_as_array ( ) <EOL> obuf . write ( arr8 . tostring ( order = '<STR_LIT:C>' ) ) <EOL> if not ( output is None ) : <EOL> return None <EOL> return obuf . getvalue ( ) <EOL> def get_rgb_image_as_buffer ( self , output = None , format = '<STR_LIT>' , quality = <NUM_LIT> ) : <EOL> if not have_PIL : <EOL> raise ImageViewCvError ( "<STR_LIT>" ) <EOL> if self . surface is None : <EOL> raise ImageViewCvError ( "<STR_LIT>" ) <EOL> ibuf = output <EOL> if ibuf is None : <EOL> ibuf = BytesIO ( ) <EOL> image = PILimage . fromarray ( self . surface ) <EOL> image . save ( ibuf , format = format , quality = quality ) <EOL> if output is not None : <EOL> return None <EOL> return ibuf . getvalue ( ) <EOL> def get_rgb_image_as_bytes ( self , format = '<STR_LIT>' , quality = <NUM_LIT> ) : <EOL> buf = self . get_rgb_image_as_buffer ( format = format , quality = quality ) <EOL> return buf <EOL> def save_rgb_image_as_file ( self , filepath , format = '<STR_LIT>' , quality = <NUM_LIT> ) : <EOL> if not have_PIL : <EOL> raise ImageViewCvError ( "<STR_LIT>" ) <EOL> if self . surface is None : <EOL> raise ImageViewCvError ( "<STR_LIT>" ) <EOL> with open ( filepath , '<STR_LIT:w>' ) as out_f : <EOL> self . get_rgb_image_as_buffer ( output = out_f , format = format , <EOL> quality = quality ) <EOL> self . logger . debug ( "<STR_LIT>" % ( format , filepath ) ) <EOL> def update_image ( self ) : <EOL> self . logger . warning ( "<STR_LIT>" ) <EOL> return False <EOL> def set_cursor ( self , cursor ) : <EOL> self . logger . warning ( "<STR_LIT>" ) <EOL> def define_cursor ( self , ctype , cursor ) : <EOL> self . cursor [ ctype ] = cursor <EOL> def get_cursor ( self , ctype ) : <EOL> return self . cursor [ ctype ] <EOL> def switch_cursor ( self , ctype ) : <EOL> self . set_cursor ( self . cursor [ ctype ] ) <EOL> def get_rgb_order ( self ) : <EOL> return self . _rgb_order <EOL> def onscreen_message ( self , text , delay = None ) : <EOL> self . logger . warning ( "<STR_LIT>" ) <EOL> def show_pan_mark ( self , tf ) : <EOL> self . t_ . set ( show_pan_position = tf ) <EOL> self . redraw ( whence = <NUM_LIT:3> ) </s>
