B
    0\$                 @   s   d dl Z d dlmZ ddlmZ e jjdkr6d dlZnd dlZda	dd Z
dd	 ZG d
d deZG dd deZdd Zdd ZdS )    N)BytesIO   )Image   c              C   sP   t d krLy(tdd} tjd| jj d da W n tjk
rJ   da Y nX t S )N1)r   r   zPIL:%d)datar   r   )_pilbitmap_okr   newtkinterBitmapImageimidTclError)r    r   *lib/python3.7/site-packages/PIL/ImageTk.py_pilbitmap_check-   s    
r   c             C   s@   d }d| kr|  d}nd| kr.t|  d}|r<t|S d S )Nfiler   )popr   r   open)kwsourcer   r   r   _get_image_from_kw9   s    r   c               @   sD   e Zd ZdZ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   s   |d krt |}t|drxt|drx|j}|dkr`|  y|jj}W n tk
r^   d}Y nX |j}|\|d< |d< n|}d }|dkrt|}|| _	|| _
tjf || _| jj| _|r| | d S )NmodesizePRGBwidthheight)r   Lr   ZRGBA)r   hasattrr   loadZpaletteAttributeErrorr   r   Zgetmodebase_PhotoImage__mode_PhotoImage__sizer
   r   _PhotoImage__phototkpaste)selfimager   r   r   r   r   r   __init__Z   s,    


zPhotoImage.__init__c             C   s@   | j j}d | j _y| j jdd| W n tk
r:   Y nX d S )Nr)   delete)r%   namer&   call	Exception)r(   r,   r   r   r   __del__z   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   s6  |   |j}| r(|j| jkr(|}n|| j|j}||| | jj	}y|
d| j|j W n tjk
r0   yddlm} yLt|drddlm} | }|t|d|jd n|| d W n$ tk
r   |t|d Y nX |
d| j|j W n  tttjfk
r*    Y nX Y nX d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. See :ref:`coordinate-system`. If None is given
                    instead of a tuple, all of the image is assumed.
        ZPyImagingPhotor   )
_imagingtkinterpr   )FFIZ	uintptr_tN)r!   r   Zisblockr   r#   Z	new_blockr   Zconvert2r%   r&   r-   r   r
   r    r2   r    Zcffir4   Ztkinitintcastr3   Z
interpaddrr"   ImportError)	r(   r   Zboxr)   blockr&   r2   r4   Zffir   r   r   r'      s0    
zPhotoImage.paste)NN)N)
__name__
__module____qualname____doc__r*   r/   r1   r   r   r'   r   r   r   r   r   F   s   
 
r   c               @   s:   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   sd   |d krt |}|j| _|j| _t rF|  d|jj |d< || _	n|
 |d< tjf || _d S )NzPIL:%dr   )r   r   Z_BitmapImage__moder   _BitmapImage__sizer   r!   r   r   Z_BitmapImage__imZtobitmapr
   r   _BitmapImage__photo)r(   r)   r   r   r   r   r*      s    zBitmapImage.__init__c             C   s@   | j j}d | j _y| j jdd| W n tk
r:   Y nX d S )Nr)   r+   )r?   r,   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   )r>   )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   )r>   )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).
        )r0   r?   )r(   r   r   r   r1   
  s    zBitmapImage.__str__)N)	r:   r;   r<   r=   r*   r/   r   r   r1   r   r   r   r   r      s   
r   c             C   s   | j d|  dS )z  This function is unimplemented ZPyImagingPhotoGetN)r&   r-   )Zphotor   r   r   getimage  s    r@   c             C   sH   G dd dt j}t js tdt  }|r6|| |||   dS )z!Helper for the Image.show method.c               @   s   e Zd Zdd ZdS )z_show.<locals>.UIc             S   sF   |j dkrt|d|d| _nt||d| _tjj| || jddd d S )Nr   Zwhite)Z
foregroundmaster)rA   Zblackr   )r)   ZbgZbd)r   r   r)   r   r
   Labelr*   )r(   rA   r   r   r   r   r*      s
    
z_show.<locals>.UI.__init__N)r:   r;   r<   r*   r   r   r   r   UI  s   rC   ztkinter not initializedN)r
   rB   Z_default_rootIOErrorZTopleveltitleZpack)r)   rE   rC   topr   r   r   _show  s    	
rG   )sysior   r5   r   version_infomajorr
   ZTkinterr   r   r   objectr   r   r@   rG   r   r   r   r   <module>   s   
 D