<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> import hashlib <EOL> import os <EOL> from . . compat import BytesIO , is_string <EOL> from . exceptions import UnrecognizedImageError <EOL> from . . shared import Emu , Inches , lazyproperty <EOL> class Image ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , blob , filename , image_header ) : <EOL> super ( Image , self ) . __init__ ( ) <EOL> self . _blob = blob <EOL> self . _filename = filename <EOL> self . _image_header = image_header <EOL> @ classmethod <EOL> def from_blob ( cls , blob ) : <EOL> """<STR_LIT>""" <EOL> stream = BytesIO ( blob ) <EOL> return cls . _from_stream ( stream , blob ) <EOL> @ classmethod <EOL> def from_file ( cls , image_descriptor ) : <EOL> """<STR_LIT>""" <EOL> if is_string ( image_descriptor ) : <EOL> path = image_descriptor <EOL> with open ( path , '<STR_LIT:rb>' ) as f : <EOL> blob = f . read ( ) <EOL> stream = BytesIO ( blob ) <EOL> filename = os . path . basename ( path ) <EOL> else : <EOL> stream = image_descriptor <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> blob = stream . read ( ) <EOL> filename = None <EOL> return cls . _from_stream ( stream , blob , filename ) <EOL> @ property <EOL> def blob ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _blob <EOL> @ property <EOL> def content_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _image_header . content_type <EOL> @ lazyproperty <EOL> def ext ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . splitext ( self . _filename ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> @ property <EOL> def filename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _filename <EOL> @ property <EOL> def px_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _image_header . px_width <EOL> @ property <EOL> def px_height ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _image_header . px_height <EOL> @ property <EOL> def horz_dpi ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _image_header . horz_dpi <EOL> @ property <EOL> def vert_dpi ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _image_header . vert_dpi <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return Inches ( self . px_width / self . horz_dpi ) <EOL> @ property <EOL> def height ( self ) : <EOL> """<STR_LIT>""" <EOL> return Inches ( self . px_height / self . vert_dpi ) <EOL> def scaled_dimensions ( self , width = None , height = None ) : <EOL> """<STR_LIT>""" <EOL> if width is None and height is None : <EOL> return self . width , self . height <EOL> if width is None : <EOL> scaling_factor = float ( height ) / float ( self . height ) <EOL> width = round ( self . width * scaling_factor ) <EOL> if height is None : <EOL> scaling_factor = float ( width ) / float ( self . width ) <EOL> height = round ( self . height * scaling_factor ) <EOL> return Emu ( width ) , Emu ( height ) <EOL> @ lazyproperty <EOL> def sha1 ( self ) : <EOL> """<STR_LIT>""" <EOL> return hashlib . sha1 ( self . _blob ) . hexdigest ( ) <EOL> @ classmethod <EOL> def _from_stream ( cls , stream , blob , filename = None ) : <EOL> """<STR_LIT>""" <EOL> image_header = _ImageHeaderFactory ( stream ) <EOL> if filename is None : <EOL> filename = '<STR_LIT>' % image_header . default_ext <EOL> return cls ( blob , filename , image_header ) <EOL> def _ImageHeaderFactory ( stream ) : <EOL> """<STR_LIT>""" <EOL> from docx . image import SIGNATURES <EOL> def read_32 ( stream ) : <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> return stream . read ( <NUM_LIT:32> ) <EOL> header = read_32 ( stream ) <EOL> for cls , offset , signature_bytes in SIGNATURES : <EOL> end = offset + len ( signature_bytes ) <EOL> found_bytes = header [ offset : end ] <EOL> if found_bytes == signature_bytes : <EOL> return cls . from_stream ( stream ) <EOL> raise UnrecognizedImageError <EOL> class BaseImageHeader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , px_width , px_height , horz_dpi , vert_dpi ) : <EOL> self . _px_width = px_width <EOL> self . _px_height = px_height <EOL> self . _horz_dpi = horz_dpi <EOL> self . _vert_dpi = vert_dpi <EOL> @ property <EOL> def content_type ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> raise NotImplementedError ( msg ) <EOL> @ property <EOL> def default_ext ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> raise NotImplementedError ( msg ) <EOL> @ property <EOL> def px_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _px_width <EOL> @ property <EOL> def px_height ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _px_height <EOL> @ property <EOL> def horz_dpi ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _horz_dpi <EOL> @ property <EOL> def vert_dpi ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _vert_dpi </s>
