def image_save_for_web(image, fp=None, format=None):
 opt = dict(format=(image.format or format))
 if (image.format == 'PNG'):
  opt.update(optimize=True)
  alpha = False
  if ((image.mode in ('RGBA', 'LA')) or ((image.mode == 'P') and ('transparency' in image.info))):
   alpha = image.convert('RGBA').split()[(-1)]
  if (image.mode != 'P'):
   image = image.convert('RGBA').convert('P', palette=Image.WEB, colors=256)
  if alpha:
   image.putalpha(alpha)
 elif (image.format == 'JPEG'):
  opt.update(optimize=True, quality=80)
 if fp:
  image.save(fp, **opt)
 else:
  img = StringIO.StringIO()
  image.save(img, **opt)
  return img.getvalue()

def image_save_for_web ( image , fp = None , format = None ) : opt = dict ( format = ( image . format or format ) ) if ( image . format == 'PNG' ) : opt . update ( optimize = True ) alpha = False if ( ( image . mode in ( 'RGBA' , 'LA' ) ) or ( ( image . mode == 'P' ) and ( 'transparency' in image . info ) ) ) : alpha = image . convert ( 'RGBA' ) . split ( ) [ ( - 1 ) ] if ( image . mode != 'P' ) : image = image . convert ( 'RGBA' ) . convert ( 'P' , palette = Image . WEB , colors = 256 ) if alpha : image . putalpha ( alpha ) elif ( image . format == 'JPEG' ) : opt . update ( optimize = True , quality = 80 ) if fp : image . save ( fp , ** opt ) else : img = StringIO . StringIO ( ) image . save ( img , ** opt ) return img . getvalue ( )
save image optimized for web usage .