<s> from io import BytesIO <EOL> from os import path <EOL> from PIL import Image <EOL> TEMPLATES = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> } <EOL> def blit_source_image ( output , template , image , panel ) : <EOL> """<STR_LIT>""" <EOL> img = Image . open ( image ) <EOL> screen = TEMPLATES [ template ] [ '<STR_LIT>' ] <EOL> factor = float ( screen [ <NUM_LIT:0> ] ) / float ( img . size [ <NUM_LIT:0> ] ) <EOL> dimensions = [ int ( i * factor ) for i in img . size ] <EOL> if panel : <EOL> dimensions [ <NUM_LIT:1> ] -= TEMPLATES [ template ] [ '<STR_LIT>' ] <EOL> img = img . resize ( dimensions , Image . ANTIALIAS ) <EOL> img = img . crop ( [ <NUM_LIT:0> , <NUM_LIT:0> ] + [ min ( * i ) for i in zip ( dimensions , screen ) ] ) <EOL> offset = list ( TEMPLATES [ template ] [ '<STR_LIT>' ] ) <EOL> if panel : <EOL> offset [ <NUM_LIT:1> ] += TEMPLATES [ template ] [ '<STR_LIT>' ] <EOL> output . paste ( img , tuple ( offset ) ) <EOL> def blit_template_image ( output , template , filename ) : <EOL> """<STR_LIT>""" <EOL> img = Image . open ( path . join ( path . dirname ( __file__ ) , '<STR_LIT>' , template , filename ) ) <EOL> return Image . alpha_composite ( output , img ) <EOL> def generate_device_art ( template , image , panel , glossy ) : <EOL> """<STR_LIT>""" <EOL> output = Image . new ( '<STR_LIT>' , TEMPLATES [ template ] [ '<STR_LIT>' ] ) <EOL> blit_source_image ( output , template , image , panel ) <EOL> if panel : <EOL> output = blit_template_image ( output , template , '<STR_LIT>' ) <EOL> output = blit_template_image ( output , template , '<STR_LIT>' ) <EOL> if glossy : <EOL> output = blit_template_image ( output , template , '<STR_LIT>' ) <EOL> response = BytesIO ( ) <EOL> output . save ( response , format = '<STR_LIT>' ) <EOL> return response . getvalue ( ) </s>
