ó
P'—^c           @   sW  d  d l  Z  d  d l Z d  d l Z d  d l m Z d d l m Z m Z d d l m	 Z	 m
 Z
 m Z m Z d g Z d d g Z i d d	 6Z e d
 „ Z d „  Z d a d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d e d „ Z  d e f d „  ƒ  YZ! d „  Z" d d e d d „ Z# d S(   iÿÿÿÿN(   t   UnsupportedOperationi   (   t   UnknownExtensiont   UnknownFormat(   t   Imaget	   ImageFilet   StringIOt   string_typest   PNGt   GIFs   .jpgt   JPEGc         K   s   t  |  t ƒ  | | | ƒ S(   N(   t
   save_imageR   (   t   imgt   formatt   autoconvertt   options(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   img_to_fobj   s    c         C   s   |  j  d ƒ t j |  ƒ S(   Ni    (   t   seekR   t   open(   t   target(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt
   open_image   s    i    c           C   s$   t  d k  r  t j ƒ  d a  t St S(   sÜ   Loads the standard PIL file format drivers. Returns True if ``preinit()``
    was called (and there's a potential that more drivers were loaded) or False
    if there is no possibility that new drivers were loaded.

    i   (   t	   _pil_initR   t   preinitt   Truet   False(    (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   _preinit_pil   s
    
c           C   s+   t  ƒ  t d k  r' t j ƒ  d a t St S(   sÐ   Loads all PIL file format drivers. Returns True if ``init()`` was called
    (and there's a potential that more drivers were loaded) or False if there is
    no possibility that new drivers were loaded.

    i   (   R   R   R   t   initR   R   (    (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt	   _init_pil*   s    
c         C   s   t  j j |  j ƒ  ƒ S(   N(   R   t	   EXTENSIONt   gett   lower(   t	   extension(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   _extension_to_format9   s    c         C   su   |  rq |  j  ƒ  }  |  t k r> t |  } | t j k r> | Sn  x0 t j j ƒ  D] \ } } | |  k rN | SqN Wn  d  S(   N(   t   uppert   DEFAULT_EXTENSIONSR   R   t   itemst   None(   R   t   extt   kt   v(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   _format_to_extension=   s    
c         C   sE   y' d |  p d } t  j | ƒ d } Wn t k
 r@ d  } n X| S(   Ns   a%st    i    (   t	   mimetypest
   guess_typet
   IndexErrorR#   (   R$   t   filenamet   mimetype(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   extension_to_mimetypeM   s    
c         C   s   t  t |  ƒ ƒ S(   N(   R.   t   format_to_extension(   R   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   format_to_mimetypeV   s    c         C   sc   t  |  ƒ } | r+ t ƒ  r+ t  |  ƒ } n  | rJ t ƒ  rJ t  |  ƒ } n  | s_ t |  ƒ ‚ n  | S(   sD   Returns the format that corresponds to the provided extension.

    (   R   R   R   R   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   extension_to_formatZ   s    c         C   sr   d } |  rY t |  ƒ } | r7 t ƒ  r7 t |  ƒ } n  | rY t ƒ  rY t |  ƒ } qY n  | sn t |  ƒ ‚ n  | S(   sC   Returns the first extension that matches the provided format.

    N(   R#   R'   R   R   R   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyR/   h   s    c         C   s¶   t  j j |  ƒ d } y t | ƒ } Wn t k
 r? | } ns X| j ƒ  | j ƒ  k ra | } nQ y t | ƒ } Wn t k
 rŠ | } n( X| j ƒ  | j ƒ  k r¬ | } n | } | S(   Ni   (   t   ost   patht   splitextR/   R   R   R1   R   (   t   nameR   t   original_extensiont   suggested_extensionR   t   original_format(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   suggest_extensionx   s    
	
	t   FileWrapperc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   t  t |  ƒ j d | ƒ d  S(   Nt   _wrapped(   t   superR:   t   __setattr__(   t   selft   wrapped(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   __init__‘   s    c         C   s/   y |  j  j ƒ  SWn t k
 r* t ‚ n Xd  S(   N(   R;   t   filenoR    t   AttributeError(   R>   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyRA   ”   s    c         C   s   t  |  j | ƒ S(   N(   t   getattrR;   (   R>   R5   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   __getattr__š   s    c         C   s   t  |  j | | ƒ S(   N(   t   setattrR;   (   R>   R5   t   value(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyR=      s    c         C   s   t  |  j | ƒ S(   N(   t   delattrR;   (   R>   t   key(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   __delattr__    s    (   t   __name__t
   __module__R@   RA   RD   R=   RI   (    (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyR:      s
   				c   
         sˆ  ˆ p	 i  ‰ | r= t  ˆ ˆ  ƒ \ ‰ } | j ˆ ƒ | ‰ n  y ˆ j d ƒ Wn t k
 ra n X‡  ‡ ‡ f d †  } t ‡ f d †  t Dƒ ƒ rœ ˆ } n t ˆ ƒ } y | | ƒ Wn¦ t k
 r^t d ˆ k rå t	 ˆ d ƒ n d d ˆ j
 d d d ˆ j
 d ˆ j
 d ƒ } | t j k  r.‚  n  t j }	 | t _ z | | ƒ Wd	 |	 t _ Xn Xy ˆ j d ƒ Wn t k
 rƒn Xˆ S(
   sR  
    Wraps PIL's ``Image.save()`` method. There are two main benefits of using
    this function over PIL's:

    1. It gracefully handles the infamous "Suspension not allowed here" errors.
    2. It prepares the image for saving using ``prepare_image()``, which will do
        some common-sense processing given the target format.

    i    c            s'   t  ƒ   ˆ j |  ˆ  ˆ  Wd  QXd  S(   N(   t   quiett   save(   t   fp(   R   R   R   (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyRM   ½   s    
c         3   s   |  ] } t  ˆ  | ƒ Vq d  S(   N(   t
   isinstance(   t   .0t   t(   t   outfile(    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pys	   <genexpr>Ä   s    t   exifi   i   i   i   N(   t   prepare_imaget   updateR   RB   t   anyR   R:   t   IOErrort   maxt   lent   sizeR   t   MAXBLOCK(
   R   RR   R   R   R   t   save_kwargsRM   t   wrappert   new_maxblockt   old_maxblock(    (   R   R   R   RR   s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyR
   ¤   s@    
			#		RL   c           B   s    e  Z d  Z d „  Z d „  Z RS(   sŒ   
    A context manager for suppressing the stderr activity of PIL's C libraries.
    Based on http://stackoverflow.com/a/978264/155370

    c         C   sŽ   y t  j j ƒ  |  _ Wn t k
 r* d  SXy t j t j t j ƒ |  _	 Wn t
 k
 r^ d  SXt j |  j ƒ |  _ t j |  j	 |  j ƒ d  S(   N(   t   syst
   __stderr__RA   t	   stderr_fdRB   R2   R   t   devnullt   O_RDWRt   null_fdt   OSErrort   dupt   oldt   dup2(   R>   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt	   __enter__ñ   s    c         O   sf   t  |  d d  ƒ s d  St  |  d d  ƒ s, d  St j |  j |  j ƒ t j |  j ƒ t j |  j ƒ d  S(   NRe   Rh   (   RC   R#   R2   Ri   Rh   Rb   t   closeRe   (   R>   t   argst   kwargs(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   __exit__  s    (   RJ   RK   t   __doc__Rj   Rn   (    (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyRL   ë   s   	c         C   s¹  t  } i  } | j ƒ  } |  j d k r¾ | t k r6 qb| t k rµ |  j ƒ  |  j ƒ  d } t j | d „  ƒ } |  j	 d ƒ j	 d d t j
 d d ƒ}  |  j d | ƒ d | d	 <qbt } n¤ |  j d k r,| t k ry |  j d	 | d	 <Wq)t k
 rq)Xqb| t k r#|  j	 d ƒ }  qbt } n6 |  j	 d ƒ }  | d
 k rb|  j	 d d t j
 ƒ}  n  | r–d d l m } | ƒ  j |  ƒ j	 d ƒ }  n  | d k r¯t | d <n  |  | f S(   sè  
    Prepares the image for saving to the provided format by doing some
    common-sense conversions. This includes things like preserving transparency
    and quantizing. This function is used automatically by ``save_image()``
    immediately before saving unless you specify ``autoconvert=False``. It is
    provided as a utility for those doing their own processing.

    :param img: The image to prepare for saving.
    :param format: The format that the image will be saved to.

    t   RGBAiÿÿÿÿc         S   s   |  d k r d Sd S(   Ni€   iÿ   i    (    (   t   a(    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   <lambda>4  R(   t   RGBt   Pt   palettet   colorsiÿ   t   transparencyR   i   (   t
   MakeOpaqueR	   t   optimize(   R   R    t   modet   RGBA_TRANSPARENCY_FORMATSt   PALETTE_TRANSPARENCY_FORMATSt   loadt   splitR   t   evalt   convertt   ADAPTIVEt   pasteR   t   infot   KeyErrort
   processorsRx   t   process(   R   R   t   make_opaqueR\   t   alphat   maskRx   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyRT     sB    
				c         C   sn   d d l  m } |  j } | | p% g  ƒ j |  ƒ }  | pL |  j pL | pL d } | pX i  } t |  | | |  S(   Ni   (   t   ProcessorPipelineR	   (   R…   RŠ   R   R†   R   (   R   R…   R   R   R   RŠ   R8   (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   process_imageb  s    	($   R2   R)   R`   t   ioR    t
   exceptionsR   R   t   libR   R   R   R   R{   R|   R!   R   R   R   R   R   R   R   R'   R.   R0   R1   R/   R9   t   objectR:   R#   R
   RL   RT   R‹   (    (    (    s.   /tmp/pip-unpacked-wheel-gNgxnH/pilkit/utils.pyt   <module>   s4   "	
											G 	W