o
    Z#al$                     @   sd   d dl Z d dlmZ ddlmZ dadd Zdd ZG d	d
 d
ZG dd dZ	dd Z
dd ZdS )    N)BytesIO   )Imagec                  C   sT   t d u r(ztdd} tjd| jj d da W t S  tjy'   da Y t S w t S )N1)r   r   PIL:)datar   r   )_pilbitmap_okr   newtkinterBitmapImageimidTclError)r    r   -/usr/lib/python3/dist-packages/PIL/ImageTk.py_pilbitmap_check'   s   r   c                 C   s@   d }d| v r|  d}nd| v rt|  d}|rt|S d S )Nfiler   )popr   r   open)kwsourcer   r   r   _get_image_from_kw3   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 u rt |}t|dr;t|dr;|j}|dkr/|  z|jj}W n ty.   d}Y nw |j}|\|d< |d< n|}d }|dvrHt|}|| _	|| _
tjdi || _| jj| _|re| | d S d S )	NmodesizePRGBwidthheight)r   Lr   RGBAr   )r   hasattrr   loadpaletteAttributeErrorr   r   getmodebase_PhotoImage__mode_PhotoImage__sizer
   r   _PhotoImage__phototkpaste)selfimager   r   r   r   r   r   __init__U   s0   

zPhotoImage.__init__c                 C   >   | j j}d | j _z| j jdd| W d S  ty   Y d S w Nr,   delete)r(   namer)   call	Exceptionr+   r1   r   r   r   __del__u      zPhotoImage.__del__c                 C   
   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__}      
zPhotoImage.__str__c                 C   
   | j d S zU
        Get the width of the image.

        :return: The width, in pixels.
        r   r'   r9   r   r   r   r         
zPhotoImage.widthc                 C   r<   zW
        Get the height of the image.

        :return: The height, in pixels.
        r   r>   r9   r   r   r   r      r?   zPhotoImage.heightc           	      C   s&  |   |j}| r|j| jkr|}n|| j|j}||| | jj	}z|
d| j|j W dS  tjy   zKddlm} z&t|dr^ddlm} | }|t|d|jd n|| d W n tyx   |t|d Y nw |
d| j|j W Y dS  tttjfy    w w )	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.
        PyImagingPhotor   )
_imagingtkinterpr   )FFI	uintptr_tN)r"   r   isblockr   r&   	new_blockr   convert2r(   r)   r2   r   r
   r    rB   r!   cffirD   tkinitintcastrC   
interpaddrr$   ImportError)	r+   r   boxr,   blockr)   rB   rD   ffir   r   r   r*      s6   
zPhotoImage.paste)NNN)
__name__
__module____qualname____doc__r-   r5   r:   r   r   r*   r   r   r   r   r   A   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   sj   |d u rt |}|j| _|j| _t r$|  d|jj |d< || _	n|
 |d< tjdi || _d S )Nr   r   r   )r   r   _BitmapImage__moder   _BitmapImage__sizer   r"   r   r   _BitmapImage__imtobitmapr
   r   _BitmapImage__photo)r+   r,   r   r   r   r   r-      s   zBitmapImage.__init__c                 C   r.   r/   )r\   r1   r)   r2   r3   r4   r   r   r   r5      r6   zBitmapImage.__del__c                 C   r<   r=   rY   r9   r   r   r   r      r?   zBitmapImage.widthc                 C   r<   r@   r]   r9   r   r   r   r      r?   zBitmapImage.heightc                 C   r7   )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).
        )r8   r\   r9   r   r   r   r:     r;   zBitmapImage.__str__rS   )	rT   rU   rV   rW   r-   r5   r   r   r:   r   r   r   r   r      s    
r   c                 C   s4   t d|  |  f}|j}| jd| |j |S )z:Copies the contents of a PhotoImage to a PIL image memory.r    PyImagingPhotoGet)r   r	   r   r   r   r)   r2   r   )photor   rQ   r   r   r   getimage  s   r`   c                 C   sH   G dd dt j}t jstdt  }|r|| |||   dS )z!Helper for the Image.show method.c                       s   e Zd Z fddZ  ZS )z_show.<locals>.UIc                    sD   |j dkrt|d|d| _nt||d| _t j|| jddd d S )Nr   white)
foregroundmaster)rc   blackr   )r,   bgbd)r   r   r,   r   superr-   )r+   rc   r   	__class__r   r   r-      s   
z_show.<locals>.UI.__init__)rT   rU   rV   r-   __classcell__r   r   rh   r   UI  s    rk   ztkinter not initializedN)r
   Label_default_rootOSErrorTopleveltitlepack)r,   rp   rk   topr   r   r   _show  s   
rs   )r
   ior   rI   r   r   r   r   r   r   r`   rs   r   r   r   r   <module>   s    D
