<s> try : <EOL> import cv <EOL> except ImportError : <EOL> import cv2 . cv as cv <EOL> from colour import Color <EOL> from thumbor . engines import BaseEngine <EOL> from pexif import JpegFile , ExifSegment <EOL> try : <EOL> from thumbor . ext . filters import _composite <EOL> FILTERS_AVAILABLE = True <EOL> except ImportError : <EOL> FILTERS_AVAILABLE = False <EOL> FORMATS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class Engine ( BaseEngine ) : <EOL> @ property <EOL> def image_depth ( self ) : <EOL> if self . image is None : <EOL> return <NUM_LIT:8> <EOL> return cv . GetImage ( self . image ) . depth <EOL> @ property <EOL> def image_channels ( self ) : <EOL> if self . image is None : <EOL> return <NUM_LIT:3> <EOL> return self . image . channels <EOL> @ classmethod <EOL> def parse_hex_color ( cls , color ) : <EOL> try : <EOL> color = Color ( color ) . get_rgb ( ) <EOL> return tuple ( c * <NUM_LIT:255> for c in reversed ( color ) ) <EOL> except Exception : <EOL> return None <EOL> def gen_image ( self , size , color_value ) : <EOL> img0 = cv . CreateImage ( size , self . image_depth , self . image_channels ) <EOL> if color_value == '<STR_LIT>' : <EOL> color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> else : <EOL> color = self . parse_hex_color ( color_value ) <EOL> if not color : <EOL> raise ValueError ( '<STR_LIT>' % color_value ) <EOL> cv . Set ( img0 , color ) <EOL> return img0 <EOL> def create_image ( self , buffer ) : <EOL> try : <EOL> if FORMATS [ self . extension ] == '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> except KeyError : <EOL> pass <EOL> imagefiledata = cv . CreateMatHeader ( <NUM_LIT:1> , len ( buffer ) , cv . CV_8UC1 ) <EOL> cv . SetData ( imagefiledata , buffer , len ( buffer ) ) <EOL> img0 = cv . DecodeImageM ( imagefiledata , cv . CV_LOAD_IMAGE_UNCHANGED ) <EOL> if FORMATS [ self . extension ] == '<STR_LIT>' : <EOL> try : <EOL> info = JpegFile . fromString ( buffer ) . get_exif ( ) <EOL> if info : <EOL> self . exif = info . data <EOL> self . exif_marker = info . marker <EOL> except Exception : <EOL> pass <EOL> return img0 <EOL> @ property <EOL> def size ( self ) : <EOL> return cv . GetSize ( self . image ) <EOL> def normalize ( self ) : <EOL> pass <EOL> def resize ( self , width , height ) : <EOL> thumbnail = cv . CreateImage ( <EOL> ( int ( round ( width , <NUM_LIT:0> ) ) , int ( round ( height , <NUM_LIT:0> ) ) ) , <EOL> self . image_depth , <EOL> self . image_channels <EOL> ) <EOL> cv . Resize ( self . image , thumbnail , cv . CV_INTER_AREA ) <EOL> self . image = thumbnail <EOL> def crop ( self , left , top , right , bottom ) : <EOL> new_width = right - left <EOL> new_height = bottom - top <EOL> cropped = cv . CreateImage ( <EOL> ( new_width , new_height ) , self . image_depth , self . image_channels <EOL> ) <EOL> src_region = cv . GetSubRect ( self . image , ( left , top , new_width , new_height ) ) <EOL> cv . Copy ( src_region , cropped ) <EOL> self . image = cropped <EOL> def rotate ( self , degrees ) : <EOL> if ( degrees > <NUM_LIT> ) : <EOL> cv . Flip ( self . image , None , - <NUM_LIT:1> ) <EOL> degrees = degrees - <NUM_LIT> <EOL> img = self . image <EOL> size = cv . GetSize ( img ) <EOL> if ( degrees / <NUM_LIT> % <NUM_LIT:2> ) : <EOL> new_size = ( size [ <NUM_LIT:1> ] , size [ <NUM_LIT:0> ] ) <EOL> center = ( ( size [ <NUM_LIT:0> ] - <NUM_LIT:1> ) * <NUM_LIT:0.5> , ( size [ <NUM_LIT:0> ] - <NUM_LIT:1> ) * <NUM_LIT:0.5> ) <EOL> else : <EOL> new_size = size <EOL> center = ( ( size [ <NUM_LIT:0> ] - <NUM_LIT:1> ) * <NUM_LIT:0.5> , ( size [ <NUM_LIT:1> ] - <NUM_LIT:1> ) * <NUM_LIT:0.5> ) <EOL> mapMatrix = cv . CreateMat ( <NUM_LIT:2> , <NUM_LIT:3> , cv . CV_64F ) <EOL> cv . GetRotationMatrix2D ( center , degrees , <NUM_LIT:1.0> , mapMatrix ) <EOL> dst = cv . CreateImage ( new_size , self . image_depth , self . image_channels ) <EOL> cv . SetZero ( dst ) <EOL> cv . WarpAffine ( img , dst , mapMatrix ) <EOL> self . image = dst <EOL> def flip_vertically ( self ) : <EOL> cv . Flip ( self . image , None , <NUM_LIT:1> ) <EOL> def flip_horizontally ( self ) : <EOL> cv . Flip ( self . image , None , <NUM_LIT:0> ) <EOL> def read ( self , extension = None , quality = None ) : <EOL> if quality is None : <EOL> quality = self . context . config . QUALITY <EOL> options = None <EOL> extension = extension or self . extension <EOL> try : <EOL> if FORMATS [ extension ] == '<STR_LIT>' : <EOL> options = [ cv . CV_IMWRITE_JPEG_QUALITY , quality ] <EOL> except KeyError : <EOL> options = [ cv . CV_IMWRITE_JPEG_QUALITY , quality ] <EOL> data = cv . EncodeImage ( extension , self . image , options or [ ] ) . tostring ( ) <EOL> if FORMATS [ extension ] == '<STR_LIT>' and self . context . config . PRESERVE_EXIF_INFO : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> img = JpegFile . fromString ( data ) <EOL> img . _segments . insert ( <NUM_LIT:0> , ExifSegment ( self . exif_marker , None , self . exif , '<STR_LIT>' ) ) <EOL> data = img . writeString ( ) <EOL> return data <EOL> def set_image_data ( self , data ) : <EOL> cv . SetData ( self . image , data ) <EOL> def image_data_as_rgb ( self , update_image = True ) : <EOL> if self . image_channels == <NUM_LIT:4> : <EOL> mode = '<STR_LIT>' <EOL> elif self . image_channels == <NUM_LIT:3> : <EOL> mode = '<STR_LIT>' <EOL> else : <EOL> mode = '<STR_LIT>' <EOL> rgb_copy = cv . CreateImage ( ( self . image . width , self . image . height ) , <NUM_LIT:8> , <NUM_LIT:3> ) <EOL> cv . CvtColor ( self . image , rgb_copy , cv . CV_GRAY2BGR ) <EOL> self . image = rgb_copy <EOL> return mode , self . image . tostring ( ) <EOL> def draw_rectangle ( self , x , y , width , height ) : <EOL> cv . Rectangle ( self . image , ( int ( x ) , int ( y ) ) , ( int ( x + width ) , int ( y + height ) ) , cv . Scalar ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:1.0> ) ) <EOL> def convert_to_grayscale ( self ) : <EOL> if self . image_channels >= <NUM_LIT:3> : <EOL> grayscaled = cv . CreateImage ( ( self . image . width , self . image . height ) , self . image_depth , <NUM_LIT:1> ) <EOL> cv . CvtColor ( self . image , grayscaled , cv . CV_BGRA2GRAY ) <EOL> self . image = grayscaled <EOL> def paste ( self , other_engine , pos , merge = True ) : <EOL> if merge and not FILTERS_AVAILABLE : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> self . enable_alpha ( ) <EOL> other_engine . enable_alpha ( ) <EOL> sz = self . size <EOL> other_size = other_engine . size <EOL> mode , data = self . image_data_as_rgb ( ) <EOL> other_mode , other_data = other_engine . image_data_as_rgb ( ) <EOL> imgdata = _composite . apply ( <EOL> mode , data , sz [ <NUM_LIT:0> ] , sz [ <NUM_LIT:1> ] , <EOL> other_data , other_size [ <NUM_LIT:0> ] , other_size [ <NUM_LIT:1> ] , pos [ <NUM_LIT:0> ] , pos [ <NUM_LIT:1> ] , merge ) <EOL> self . set_image_data ( imgdata ) <EOL> def enable_alpha ( self ) : <EOL> if self . image_channels < <NUM_LIT:4> : <EOL> with_alpha = cv . CreateImage ( <EOL> ( self . image . width , self . image . height ) , self . image_depth , <NUM_LIT:4> <EOL> ) <EOL> if self . image_channels == <NUM_LIT:3> : <EOL> cv . CvtColor ( self . image , with_alpha , cv . CV_BGR2BGRA ) <EOL> else : <EOL> cv . CvtColor ( self . image , with_alpha , cv . CV_GRAY2BGRA ) <EOL> self . image = with_alpha </s>
