ó
a#nSc        	   @   s„  d  Z  d Z y d d l TWn e k
 r3 d Z n Xd Z d d l m Z d d l Z d d l Z d d l	 Z d d l
 Z d d l Z d d l Z d d l Z e j d d d „ Z e j d d	 „ Z d d d d d
 „ Z e rì d „  Z n  e rþ d „  Z n  e d k r€e d d d d d d g d d d d d d g d d d d d d g g ƒ Z e e ƒ Z e j d ƒ Z e j d ƒ n  d S(   s$    tools for working with PIL images

i    iÿÿÿÿ(   t   *i   (   t   ImageNc         C   s‹   | ru | r* t  |  j d ƒ | d } n t  |  j d ƒ | d } t |  j d | ƒ t |  j d j ƒ f } n  |  j | | ƒ } | S(   s  Resizes an image to fit a given space.  The new image is returned.

    **Arguments**

      - origImg: a PIL image

      - newSize: the requested size (either a 2-tuple or a list 2 elements long)

      - filter: the filter to be used in resizing the image

      - maintainAspect: toggles maintaining the aspect ratio of the image

      - prioritiyX: (only meaningful when _maintainAspect_ is nonzero)
           if nonzero, the X size will be given priority in setting the new size,
           otherwise the Y size will take priority

    **Returns**

      a PIL image

    **Notes**

      - if maintainAspect is nonzero, the aspect ratio of the image
       will not be changed.  This implies that the final image may not
       actually be the requested size.

  i    i   (   t   floatt   sizet   intt	   scaleFactt   resize(   t   origImgt   newSizet   filtert   maintainAspectt	   priorityXR   t   newImg(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/PilTools.pyt   ResizeImage#   s    0iÿ   c         C   s=  |  j  d ƒ } t j | j | | ƒ } t t | d ƒ |  j d t | d ƒ |  j d ƒ } | d k  r© |  j t |  j d | ƒ t |  j d | ƒ f | ƒ } n |  } | d | j d } | d k rÝ | d }	 n d }	 | d | j d }
 |
 d k r|
 d } n d } | j	 | |	 | f ƒ | j  |  j ƒ S(   sã  Fits an image into a box of a particular size.

    **Arguments**

      - origImg: a PIL image

      - newSize: the requested size (either a 2-tuple or a list 2 elements long)

      - filter: the filter to be used in resizing the image

      - bgColor: the background color to start with
      
    **Returns**

      a PIL image

    **Notes**

      - there may be blank spaces around the original image in the new image,
        these will be filled with _bgColor_
        
  t   RGBi    i   i   (
   t   convertR   t   newt   modet   minR   R   R   R   t   paste(   R   R   R	   t   bgColort   tmpImgR   R   t   tImgt   xDifft   xLoct   yDifft   yLoc(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/PilTools.pyt   FitImageL   s"    =c      	   C   sã  d d  l  } | j |  | j ƒ }  | j r< | j |  ƒ }  n  |  j \ } } | r| j |  ƒ } | j |  ƒ }	 t | ƒ }
 t | ƒ } x@ t | ƒ D]2 } |  | | | |
 | <|  | |	 | | | <q” W|  |
 8}  | |
 8} | | j	 | d ƒ 7} |  | :}  n  | j
 | | d f | j ƒ } | j | ƒ } | j | ƒ } | | 9} | | } x[ t | ƒ D]M } xD t | ƒ D]6 } | | | f c | |  | | f j | j ƒ 7<qqWq^W| j d ƒ j ƒ  } t j d | | f | ƒ } | S(   Niÿÿÿÿg        i   t   BR   (   t   numpyt   arrayR   t	   transposet   shapet   argmint   argmaxt   zerost   ranget   equalt   onest   integert   astypet   tostringR   t
   fromstring(   t   datat	   scaleColst   transposeItt   minColort   maxColorR   t   nRowst   nColst
   minIndicest
   maxIndicest   minst   maxst   it   imgMatt
   deltaColort   jt   dt   img(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/PilTools.pyt   NumericMatrixToImage~   s8    	



8c         C   s@   t  |  j d |  j d ƒ } | j |  j ƒ  ƒ | j ƒ  } | S(   s”    converts a PIL image into a wxPython bitmap

      **Arguments**

        - pilImg: a PIL image

      **Returns**

        a wxPython bitmap

    i    i   (   t   wxEmptyImageR   t   SetDataR)   t   ConvertToBitmap(   t   pilImgt   wxImgt   bmp(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/PilTools.pyt   PilImgToWxBmp¨   s    c         C   sL   d d l  m  } | ƒ  } |  j | d d ƒt ƒ  } | j | j ƒ  ƒ | S(   Niÿÿÿÿ(   t   StringIOt   formatt   png(   RD   t   savet   QPixmapt   loadFromDatat   getvalue(   R@   RD   t   siot   pm(    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/PilTools.pyt   PilImgToQPixmapº   s    		t   __main__g      ð?g      à?iÈ   s   foo.gif(   iÿ   iÿ   iÿ   (   i    i    i    (   iÿ   iÿ   iÿ   (   iÈ   iÈ   (   t   __doc__t   hasWxt   qtt   ImportErrort   hasQtt   PILR   t   PIL.ImageFilet   PIL.GifImagePlugint   PIL.PngImagePlugint   PIL.JpegImagePlugint   PIL.BmpImagePlugint   PIL.TiffImagePlugint   PIL.PpmImagePlugint   BILINEARR   R   R<   RC   RM   t   __name__R   t   vsR;   R   RG   (    (    (    sV   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/utils/PilTools.pyt   <module>
   s8   
	(2(K