î
'"ìV”#  ã               @   s¤   y d  d l  Z  Wn' e k
 r9 d  d l Z e Z  [ Yn Xd  d l m Z d a d d „  Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z	 d	 d
 „  Z
 d d „  Z d S)é    N)ÚImagec              C   sg   t  d  k rc y6 t j d d ƒ }  t j d d |  j j ƒ d a  Wqc t j k
 r_ d a  Yqc Xn  t  S)NÚ1é   ÚdatazPIL:%dr   )r   r   )Ú_pilbitmap_okr   ÚnewÚtkinterÚBitmapImageÚimÚidÚTclError)r
   © r   ú-/tmp/pip-build-fj3bmf4v/Pillow/PIL/ImageTk.pyÚ_pilbitmap_check,   s    
r   c               @   sg   e  Z d  Z d Z d d d d „ Z d d „  Z d d „  Z d	 d
 „  Z d d „  Z d d d „ Z	 d S)Ú
PhotoImagea3  
    A Tkinter-compatible photo image.  This can be used
    everywhere Tkinter expects an image object.  If the image is an RGBA
    image, pixels having alpha 0 are treated as transparent.

    The constructor takes either a PIL image, or a mode and a size.
    Alternatively, you can use the **file** or **data** options to initialize
    the photo image object.

    :param image: Either a PIL image, or a mode string.  If a mode string is
                  used, a size must also be given.
    :param size: If the first argument is a mode string, this defines the size
                 of the image.
    :keyword file: A filename to load the image from (using
                   ``Image.open(file)``).
    :keyword data: An 8-bit string containing image data (as loaded from an
                   image file).
    Nc             K   sy  | d  k rw d | k r5 t  j | d ƒ } | d =qw d | k rw d d l m } t  j | | d ƒ ƒ } | d =qw n  t | d ƒ rt | d ƒ r| j } | d k râ | j ƒ  y | j j } Wqâ t k
 rÞ d } Yqâ Xn  | j	 } | \ | d	 <| d
 <n | } d  } | d k r,t  j
 | ƒ } n  | |  _ | |  _ t j |   |  _ |  j j |  _ | ru|  j | ƒ n  d  S)NÚfiler   r   )ÚBytesIOÚmodeÚsizeÚPÚRGBÚwidthÚheightr   ÚLÚRGBA)r   r   r   r   )r   ÚopenÚior   Úhasattrr   ÚloadZpaletteÚAttributeErrorr   ZgetmodebaseÚ_PhotoImage__modeÚ_PhotoImage__sizer   r   Ú_PhotoImage__photoÚtkÚpaste)ÚselfÚimager   Úkwr   r   r   r   r   Ú__init__O   s8    
	
			zPhotoImage.__init__c          
   C   sD   |  j  j } d  |  j  _ y |  j  j j d d | ƒ Wn Yn Xd  S)Nr&   Údelete)r"   Únamer#   Úcall)r%   r*   r   r   r   Ú__del__u   s    zPhotoImage.__del__c             C   s   t  |  j ƒ S)zô
        Get the Tkinter photo image identifier.  This method is automatically
        called by Tkinter whenever a PhotoImage object is passed to a Tkinter
        method.

        :return: A Tkinter photo image identifier (a string).
        )Ústrr"   )r%   r   r   r   Ú__str__}   s    zPhotoImage.__str__c             C   s   |  j  d S)zU
        Get the width of the image.

        :return: The width, in pixels.
        r   )r!   )r%   r   r   r   r   ‡   s    zPhotoImage.widthc             C   s   |  j  d S)zW
        Get the height of the image.

        :return: The height, in pixels.
        r   )r!   )r%   r   r   r   r      s    zPhotoImage.heightc             C   s=  | j  ƒ  | j } | j ƒ  r: | j |  j k r: | } n( | j |  j | j ƒ } | j | | ƒ |  j j	 } y | j
 d |  j | j ƒ Wn« t j k
 r8yr d d l m } y | j | j ƒ  d ƒ Wn( t k
 rõ | j t | ƒ d ƒ Yn X| j
 d |  j | j ƒ Wn! t t t j f k
 r3‚  Yn XYn Xd S)a  
        Paste a PIL image into the photo image.  Note that this can
        be very slow if the photo image is displayed.

        :param im: A PIL image. The size must match the target region.  If the
                   mode does not match, the image is converted to the mode of
                   the bitmap image.
        :param box: A 4-tuple defining the left, upper, right, and lower pixel
                    coordinate.  If None is given instead of a tuple, all of
                    the image is assumed.
        ZPyImagingPhotor   )Ú
_imagingtkr   N)r   r
   Zisblockr   r    Z	new_blockr   Zconvert2r"   r#   r+   r   r   r   ÚPILr/   ZtkinitZ
interpaddrr   ÚImportError)r%   r
   Zboxr&   Úblockr#   r/   r   r   r   r$   —   s&    
		zPhotoImage.paste)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r,   r.   r   r   r$   r   r   r   r   r   ;   s   &
r   c               @   sU   e  Z d  Z d Z d d d „ Z d d „  Z d d „  Z d	 d
 „  Z d d „  Z d S)r	   aå  

    A Tkinter-compatible bitmap image.  This can be used everywhere Tkinter
    expects an image object.

    The given image must have mode "1".  Pixels having value 0 are treated as
    transparent.  Options, if any, are passed on to Tkinter.  The most commonly
    used option is **foreground**, which is used to specify the color for the
    non-transparent parts.  See the Tkinter documentation for information on
    how to specify colours.

    :param image: A PIL image.
    Nc             K   sè   | d  k rw d | k r5 t  j | d ƒ } | d =qw d | k rw d d l m } t  j | | d ƒ ƒ } | d =qw n  | j |  _ | j |  _ t ƒ  rÂ | j	 ƒ  d | j
 j | d <| |  _ n | j ƒ  | d <t j |   |  _ d  S)Nr   r   r   )r   zPIL:%d)r   r   r   r   r   Z_BitmapImage__moder   Ú_BitmapImage__sizer   r   r
   r   Z_BitmapImage__imZtobitmapr   r	   Ú_BitmapImage__photo)r%   r&   r'   r   r   r   r   r(   Ð   s     
	
zBitmapImage.__init__c          
   C   sD   |  j  j } d  |  j  _ y |  j  j j d d | ƒ Wn Yn Xd  S)Nr&   r)   )r8   r*   r#   r+   )r%   r*   r   r   r   r,   é   s    zBitmapImage.__del__c             C   s   |  j  d S)zU
        Get the width of the image.

        :return: The width, in pixels.
        r   )r7   )r%   r   r   r   r   ñ   s    zBitmapImage.widthc             C   s   |  j  d S)zW
        Get the height of the image.

        :return: The height, in pixels.
        r   )r7   )r%   r   r   r   r   ù   s    zBitmapImage.heightc             C   s   t  |  j ƒ S)z÷
        Get the Tkinter bitmap image identifier.  This method is automatically
        called by Tkinter whenever a BitmapImage object is passed to a Tkinter
        method.

        :return: A Tkinter bitmap image identifier (a string).
        )r-   r8   )r%   r   r   r   r.     s    zBitmapImage.__str__)	r3   r4   r5   r6   r(   r,   r   r   r.   r   r   r   r   r	   Á   s   r	   c             C   s   |  j  j d |  ƒ d S)z:Copies the contents of a PhotoImage to a PIL image memory.ZPyImagingPhotoGetN)r#   r+   )Zphotor   r   r   Úgetimage  s    r9   c             C   sj   Gd d „  d t  j ƒ } t  j s1 t d ƒ ‚ n  t  j ƒ  } | rS | j | ƒ n  | | |  ƒ j ƒ  d  S)Nc               @   s   e  Z d  Z d d „  Z d S)z_show.<locals>.UIc          	   S   sn   | j  d k r- t | d d d | ƒ|  _ n t | d | ƒ|  _ t j j |  | d |  j d d d d	 ƒd  S)
Nr   Z
foregroundZwhiteÚmasterr&   ÚbgZblackZbdr   )r   r	   r&   r   r   ÚLabelr(   )r%   r:   r
   r   r   r   r(     s
    z_show.<locals>.UI.__init__N)r3   r4   r5   r(   r   r   r   r   ÚUI  s   r=   ztkinter not initialized)r   r<   Z_default_rootÚIOErrorZToplevelÚtitleÚpack)r&   r?   r=   Útopr   r   r   Ú_show  s    		rB   )r   r1   ÚTkinterr0   r   r   r   Úobjectr   r	   r9   rB   r   r   r   r   Ú<module>   s   †K