<s> """<STR_LIT>""" <EOL> import logging <EOL> from PIL import ImageDraw , Image , ImageEnhance <EOL> from sigal import signals <EOL> logger = logging . getLogger ( __name__ ) <EOL> def reduce_opacity ( im , opacity ) : <EOL> """<STR_LIT>""" <EOL> assert opacity >= <NUM_LIT:0> and opacity <= <NUM_LIT:1> <EOL> if im . mode != '<STR_LIT>' : <EOL> im = im . convert ( '<STR_LIT>' ) <EOL> else : <EOL> im = im . copy ( ) <EOL> alpha = im . split ( ) [ <NUM_LIT:3> ] <EOL> alpha = ImageEnhance . Brightness ( alpha ) . enhance ( opacity ) <EOL> im . putalpha ( alpha ) <EOL> return im <EOL> def watermark ( im , mark , position , opacity = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if opacity < <NUM_LIT:1> : <EOL> mark = reduce_opacity ( mark , opacity ) <EOL> if im . mode != '<STR_LIT>' : <EOL> im = im . convert ( '<STR_LIT>' ) <EOL> layer = Image . new ( '<STR_LIT>' , im . size , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> if position == '<STR_LIT>' : <EOL> for y in range ( <NUM_LIT:0> , im . size [ <NUM_LIT:1> ] , mark . size [ <NUM_LIT:1> ] ) : <EOL> for x in range ( <NUM_LIT:0> , im . size [ <NUM_LIT:0> ] , mark . size [ <NUM_LIT:0> ] ) : <EOL> layer . paste ( mark , ( x , y ) ) <EOL> elif position == '<STR_LIT>' : <EOL> ratio = min ( <EOL> float ( im . size [ <NUM_LIT:0> ] ) / mark . size [ <NUM_LIT:0> ] , float ( im . size [ <NUM_LIT:1> ] ) / mark . size [ <NUM_LIT:1> ] ) <EOL> w = int ( mark . size [ <NUM_LIT:0> ] * ratio ) <EOL> h = int ( mark . size [ <NUM_LIT:1> ] * ratio ) <EOL> mark = mark . resize ( ( w , h ) ) <EOL> layer . paste ( mark , ( ( im . size [ <NUM_LIT:0> ] - w ) / <NUM_LIT:2> , ( im . size [ <NUM_LIT:1> ] - h ) / <NUM_LIT:2> ) ) <EOL> else : <EOL> layer . paste ( mark , position ) <EOL> return Image . composite ( layer , im , layer ) <EOL> def add_watermark ( img , settings = None ) : <EOL> logger . debug ( '<STR_LIT>' , img ) <EOL> mark = Image . open ( settings [ '<STR_LIT>' ] ) <EOL> position = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> opacity = settings . get ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> return watermark ( img , mark , position , opacity ) <EOL> def register ( settings ) : <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> signals . img_resized . connect ( add_watermark ) <EOL> else : <EOL> logger . warning ( '<STR_LIT>' ) </s>
