o
    _#ûaý  ã                   @   s$  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ g Zd)dd„Z	d*dd„Z
G d	d
„ d
ƒZG dd„ deƒZejdkr@e	eƒ G dd„ deƒZejdkrQe	eƒ G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZejdvr³e d¡r�e	eƒ e d¡r˜e	eƒ e d ¡r¡e	eƒ e d!¡rªe	eƒ e d"¡r³e	eƒ G d#d$„ d$eƒZzd d%lmZ W n	 eyÌ   Y nw e	eƒ ed&k�reejƒd'k råed(ƒ e ¡  e  ejd ¡�Z!ee
e!gejd'd… ¢R Ž ƒ W d  ƒ dS 1 �s	w   Y  dS dS )+é    N©Úquote)ÚImageé   c                 C   sN   z
t | tƒr	| ƒ } W n	 ty   Y nw |dkrt | ¡ dS t d| ¡ dS )a  
    The :py:func:`register` function is used to register additional viewers.

    :param viewer: The viewer to be registered.
    :param order:
        Zero or a negative integer to prepend this viewer to the list,
        a positive integer to append it.
    r   N)Ú
issubclassÚViewerÚ	TypeErrorÚ_viewersÚappendÚinsert)ÚviewerÚorder© r   ú//usr/lib/python3/dist-packages/PIL/ImageShow.pyÚregister   s   	
€ÿr   c                 K   s*   t D ]}|j| fd|i|¤Žr dS qdS )a  
    Display a given image.

    :param image: An image object.
    :param title: Optional title. Not all viewers can display the title.
    :param \**options: Additional viewer options.
    :returns: ``True`` if a suitable viewer was found, ``False`` otherwise.
    Útitler   r   )r	   Úshow)Úimager   Úoptionsr   r   r   r   r   -   s
   	ÿr   c                   @   sT   e Zd ZdZdd„ ZdZ	 i Z	 dd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ ZdS )r   zBase class for viewers.c                 K   sP   |j dv s| jdkr|j dv st |j ¡}|j |kr| |¡}| j|fi |¤ŽS )zƒ
        The main function for displaying an image.
        Converts the given image to the target format and displays it.
        )Ú1ÚRGBAÚPNG)zI;16ÚLA)ÚmodeÚformatr   ÚgetmodebaseÚconvertÚ
show_image)Úselfr   r   Úbaser   r   r   r   A   s   


zViewer.showNc                 C   s   | j S )z3Return format name, or ``None`` to save as PGM/PPM.)r   ©r   r   r   r   r   Ú
get_formatX   s   zViewer.get_formatc                 K   s   t ‚)zj
        Returns the command used to display the file.
        Not implemented in the base class.
        )ÚNotImplementedError©r   Úfiler   r   r   r   Úget_command\   s   zViewer.get_commandc                 C   s   |j dd|  |¡i| j¤ŽS )z+Save to temporary file and return filename.r   Nr   )Ú_dumpr!   r   r    r   r   r   Ú
save_imagec   s   zViewer.save_imagec                 K   s   | j |  |¡fi |¤ŽS )zDisplay the given image.)Ú	show_filer'   ©r   r   r   r   r   r   r   g   s   zViewer.show_imagec                 K   s   t  | j|fi |¤Ž¡ dS )úDisplay given filer   )ÚosÚsystemr%   r#   r   r   r   r(   k   s   zViewer.show_filec                 C   s   t  tjdd|g¡ d S )Nz-cz<import os, sys, time; time.sleep(20); os.remove(sys.argv[1]))Ú
subprocessÚPopenÚsysÚ
executable)r   r$   r   r   r   Ú_remove_file_after_delayp   s   üÿzViewer._remove_file_after_delay)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r%   r'   r   r(   r1   r   r   r   r   r   <   s    r   c                   @   s$   e Zd ZdZdZddiZdd„ ZdS )ÚWindowsViewerzNThe default viewer on Windows is the default system application for PNG files.r   Úcompress_levelr   c                 K   s   d|› d|› d�S )Nzstart "Pillow" /WAIT "z)" && ping -n 2 127.0.0.1 >NUL && del /f "ú"r   r#   r   r   r   r%   „   s   þÿzWindowsViewer.get_commandN)r2   r3   r4   r5   r   r   r%   r   r   r   r   r6   ~   s
    r6   Úwin32c                   @   s,   e Zd ZdZdZddiZdd„ Zdd„ Zd	S )
Ú	MacViewerz2The default viewer on macOS using ``Preview.app``.r   r7   r   c                 K   s(   d}d|› dt |ƒ› dt |ƒ› d�}|S )Nzopen -a Preview.appú(ú z; sleep 20; rm -f ú)&r   ©r   r$   r   Úcommandr   r   r   r%   –   s    zMacViewer.get_commandc                 K   s    t  ddd|g¡ |  |¡ dS )r*   Úopenz-azPreview.appr   )r-   Úcallr1   r#   r   r   r   r(   �   s   
zMacViewer.show_fileN)r2   r3   r4   r5   r   r   r%   r(   r   r   r   r   r:   �   s    r:   Údarwinc                   @   s    e Zd ZdZddiZdd„ ZdS )Ú
UnixViewerr   r7   r   c                 K   s6   | j |fi |¤Žd }d|› dt|ƒ› dt|ƒ› d�S )Nr   r;   r<   z; rm -f r=   )Úget_command_exr   r>   r   r   r   r%   ¬   s    zUnixViewer.get_commandN)r2   r3   r4   r   r   r%   r   r   r   r   rC   ¨   s    rC   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )Ú	XDGViewerz3
    The freedesktop.org ``xdg-open`` command.
    c                 K   s   d }}||fS )Núxdg-openr   )r   r$   r   r?   r0   r   r   r   rD   ¶   s   zXDGViewer.get_command_exc                 K   s   t  d|g¡ |  |¡ dS )r*   rG   r   )r-   r.   r1   r#   r   r   r   r(   º   s   
zXDGViewer.show_fileN©r2   r3   r4   r5   rD   r(   r   r   r   r   rF   ±   s    rF   c                   @   ó"   e Zd ZdZddd„Zdd„ ZdS )ÚDisplayViewerz`
    The ImageMagick ``display`` command.
    This viewer supports the ``title`` parameter.
    Nc                 K   ó&   d }}|r|dt |ƒ› �7 }||fS )NÚdisplayú -name r   ©r   r$   r   r   r?   r0   r   r   r   rD   Ç   s   zDisplayViewer.get_command_exc                 K   ó@   dg}d|v r|d|d g7 }|  |¡ t |¡ t |¡ dS )r*   rL   r   ú-namer   ©r
   r-   r.   r+   Úremove©r   r$   r   Úargsr   r   r   r(   Í   ó   


zDisplayViewer.show_file©NrH   r   r   r   r   rJ   Á   s    
rJ   c                   @   rE   )ÚGmDisplayViewerz*The GraphicsMagick ``gm display`` command.c                 K   ó   d}d}||fS )NÚgmz
gm displayr   ©r   r$   r   r0   r?   r   r   r   rD   Ü   ó   zGmDisplayViewer.get_command_exc                 K   ó   t  dd|g¡ t |¡ dS )r*   rY   rL   r   ©r-   r.   r+   rR   r#   r   r   r   r(   á   ó   
zGmDisplayViewer.show_fileNrH   r   r   r   r   rW   Ù   ó    rW   c                   @   rE   )Ú	EogViewerz'The GNOME Image Viewer ``eog`` command.c                 K   rX   )NÚeogzeog -nr   rZ   r   r   r   rD   ë   r[   zEogViewer.get_command_exc                 K   r\   )r*   ra   z-nr   r]   r#   r   r   r   r(   ð   r^   zEogViewer.show_fileNrH   r   r   r   r   r`   è   r_   r`   c                   @   rI   )ÚXVViewerzX
    The X Viewer ``xv`` command.
    This viewer supports the ``title`` parameter.
    Nc                 K   rK   )NÚxvrM   r   rN   r   r   r   rD   ý   s   zXVViewer.get_command_exc                 K   rO   )r*   rc   r   rP   r   rQ   rS   r   r   r   r(     rU   zXVViewer.show_filerV   rH   r   r   r   r   rb   ÷   s    
rb   )r9   rB   rG   rL   rY   ra   rc   c                   @   s   e Zd ZdZdd„ ZdS )ÚIPythonViewerz!The viewer for IPython frontends.c                 K   s   t |ƒ dS )Nr   )Úipython_displayr)   r   r   r   r   !  s   zIPythonViewer.show_imageN)r2   r3   r4   r5   r   r   r   r   r   rd     s    rd   )rL   Ú__main__é   z.Syntax: python3 ImageShow.py imagefile [title])r   rV   )"r+   Úshutilr-   r/   Úshlexr   ÚPILr   r	   r   r   r   r6   Úplatformr:   rC   rF   rJ   rW   r`   rb   Úwhichrd   ÚIPython.displayrL   re   ÚImportErrorr2   ÚlenÚargvÚprintÚexitr@   Úimr   r   r   r   Ú<module>   s`   

B

	





ÿ
 $ÿú