#NEWLINE# def save_image( image , format = 'JPEG' , **options ) : #NEWLINE# #INDENT# '\n    Save a PIL image to memory and return a StringIO instance.\n    ' #NEWLINE# destination = StringIO ( ) #NEWLINE# if ( format == 'JPEG' ) : #NEWLINE# #INDENT# options . setdefault ( 'quality' , 85 ) #NEWLINE# try : #NEWLINE# #INDENT# image . save ( destination , format = format , optimize = 1 , ** options ) #NEWLINE# #UNINDENT# except IOError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# image . save ( destination , format = format , ** options ) #NEWLINE# if hasattr ( destination , 'seek' ) : #NEWLINE# #INDENT# destination . seek ( 0 ) #NEWLINE# #UNINDENT# return ContentFile ( destination . read ( ) )