a
    LˆcÙ+  ã                   @   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 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r®e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 ¡�rBeeƒ e d!¡�rVeeƒ e d"¡�rjeeƒ e d#¡�r~eeƒ G d$d%„ d%eƒZzd d&lmZ W n e�y²   Y n
0 eeƒ ed'k�r6eejƒd(k �ræe d)ƒ e !¡  e "ejd ¡�.Z#e ee#gejd(d… ¢R Ž ƒ W d  ƒ n1 �s,0    Y  dS ),é    N©Úquote)ÚImageé   )Ú	deprecatec                 C   sL   zt | tƒr| ƒ } W n ty&   Y n0 |dkr<t | ¡ nt d| ¡ dS )a$  
    The :py:func:`register` function is used to register additional viewers::

        from PIL import ImageShow
        ImageShow.register(MyViewer())  # MyViewer will be used as a last resort
        ImageShow.register(MySecondViewer(), 0)  # MySecondViewer will be prioritised
        ImageShow.register(ImageShow.XVViewer(), 0)  # XVViewer will be prioritised

    :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   úP/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-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.
    ÚtitleTF)r
   Úshow)Úimager   Úoptionsr   r   r   r   r   4   s    	r   c                   @   sJ   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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.
        )Ú1ZRGBAÚPNG)zI;16ZLA)ÚmodeÚformatr   ZgetmodebaseÚconvertÚ
show_image)Úselfr   r   Úbaser   r   r   r   H   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_format_   s    zViewer.get_formatc                 K   s   t ‚dS )zj
        Returns the command used to display the file.
        Not implemented in the base class.
        N)ÚNotImplementedError©r   Úfiler   r   r   r   Úget_commandc   s    zViewer.get_commandc                 C   s   |j f d|  |¡i| j¤ŽS )z+Save to temporary file and return filename.r   )Ú_dumpr   r   r   r   r   r   Ú
save_imagej   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   n   s    zViewer.show_imagec                 K   sL   |du r0d|v r(t dddƒ | d¡}ntdƒ‚t | j|fi |¤Ž¡ dS )úà
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and will be removed in Pillow 10.0.0 (2023-07-01). ``path`` should be used
        instead.
        Nr"   úThe 'file' argumenté
   ú'path'ú!Missing required argument: 'path'r   )r   Úpopr	   ÚosÚsystemr#   ©r   Úpathr   r   r   r   r&   r   s    zViewer.show_file)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r#   r%   r   r&   r   r   r   r   r   C   s   r   c                   @   s&   e Zd ZdZdZdddœZdd„ ZdS )	ÚWindowsViewerzNThe default viewer on Windows is the default system application for PNG files.r   r   T©Zcompress_levelZsave_allc                 K   s   d|› d|› d�S )Nzstart "Pillow" /WAIT "z)" && ping -n 4 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                   @   s0   e Zd ZdZdZdddœZdd„ Zdd	d
„ZdS )Ú	MacViewerz2The default viewer on macOS using ``Preview.app``.r   r   Tr7   c                 K   s(   d}d|› dt |ƒ› dt |ƒ› d�}|S )Nzopen -a Preview.appú(ú z; sleep 20; rm -f z)&r   ©r   r"   r   Úcommandr   r   r   r#   Ÿ   s     zMacViewer.get_commandNc                 K   sl   |du r0d|v r(t dddƒ | d¡}ntdƒ‚t ddd	|g¡ tjpPt d
¡}|rht 	|dd|g¡ dS )r(   Nr"   r)   r*   r+   r,   Úopenz-azPreview.appZpython3z-cz<import os, sys, time; time.sleep(20); os.remove(sys.argv[1])r   )
r   r-   r	   Ú
subprocessÚcallÚsysÚ
executableÚshutilÚwhichÚPopen)r   r1   r   rC   r   r   r   r&   ¦   s     üÿzMacViewer.show_file)N)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dœZdd„ ZdS )Ú
UnixViewerr   r   Tr7   c                 K   s*   | j |fi |¤Žd }d|› dt|ƒ› �S )Nr   r;   r<   )Úget_command_exr   r=   r   r   r   r#   Ê   s    zUnixViewer.get_commandN)r2   r3   r4   r   r   r#   r   r   r   r   rH   Æ   s   
rH   c                   @   s"   e Zd ZdZdd„ Zd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>   rC   r   r   r   rI   Ô   s    zXDGViewer.get_command_exNc                 K   sB   |du r0d|v r(t dddƒ | d¡}ntdƒ‚t d|g¡ dS )	r(   Nr"   r)   r*   r+   r,   rK   r   ©r   r-   r	   r@   rF   r0   r   r   r   r&   Ø   s    zXDGViewer.show_file)N©r2   r3   r4   r5   rI   r&   r   r   r   r   rJ   Ï   s   rJ   c                   @   s$   e Zd ZdZddd„Zddd„ZdS )	ÚDisplayViewerz`
    The ImageMagick ``display`` command.
    This viewer supports the ``title`` parameter.
    Nc                 K   s&   d }}|r|dt |ƒ› �7 }||fS )NÚdisplayz -title r   ©r   r"   r   r   r>   rC   r   r   r   rI   ð   s    zDisplayViewer.get_command_exc                 K   sh   |du r0d|v r(t dddƒ | d¡}ntdƒ‚dg}| d¡}|rP|d	|g7 }| |¡ t |¡ d
S )ú©
        Display given file.

        Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
        and ``path`` should be used instead.
        Nr"   r)   r*   r+   r,   rO   r   z-titler   ©r   r-   r	   Úgetr   r@   rF   ©r   r1   r   Úargsr   r   r   r   r&   ö   s    


zDisplayViewer.show_file)N)NrM   r   r   r   r   rN   ê   s   
rN   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )ÚGmDisplayViewerz*The GraphicsMagick ``gm display`` command.c                 K   s   d}d}||fS )NÚgmz
gm displayr   ©r   r"   r   rC   r>   r   r   r   rI     s    zGmDisplayViewer.get_command_exNc                 K   sD   |du r0d|v r(t dddƒ | d¡}ntdƒ‚t dd|g¡ d	S )
rQ   Nr"   r)   r*   r+   r,   rW   rO   r   rL   r0   r   r   r   r&     s    zGmDisplayViewer.show_file)NrM   r   r   r   r   rV     s   rV   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )Ú	EogViewerz'The GNOME Image Viewer ``eog`` command.c                 K   s   d}d}||fS )NÚeogzeog -nr   rX   r   r   r   rI   )  s    zEogViewer.get_command_exNc                 K   sD   |du r0d|v r(t dddƒ | d¡}ntdƒ‚t dd|g¡ d	S )
rQ   Nr"   r)   r*   r+   r,   rZ   z-nr   rL   r0   r   r   r   r&   .  s    zEogViewer.show_file)NrM   r   r   r   r   rY   &  s   rY   c                   @   s$   e Zd ZdZddd„Zddd„ZdS )	ÚXVViewerzX
    The X Viewer ``xv`` command.
    This viewer supports the ``title`` parameter.
    Nc                 K   s&   d }}|r|dt |ƒ› �7 }||fS )NÚxvz -name r   rP   r   r   r   rI   E  s    zXVViewer.get_command_exc                 K   sh   |du r0d|v r(t dddƒ | d¡}ntdƒ‚dg}| d¡}|rP|d	|g7 }| |¡ t |¡ d
S )rQ   Nr"   r)   r*   r+   r,   r\   r   z-namer   rR   rT   r   r   r   r&   M  s    


zXVViewer.show_file)N)NrM   r   r   r   r   r[   ?  s   
r[   )r9   rG   rK   rO   rW   rZ   r\   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   t  s    zIPythonViewer.show_imageN)r2   r3   r4   r5   r   r   r   r   r   r]   q  s   r]   )rO   Ú__main__é   z.Syntax: python3 ImageShow.py imagefile [title])r   )N)$r.   rD   r@   rB   Úshlexr   ZPILr   Z
_deprecater   r
   r   r   r   r6   Úplatformr:   rH   rJ   rN   rV   rY   r[   rE   r]   ZIPython.displayrO   r^   ÚImportErrorr2   ÚlenÚargvÚprintÚexitr?   Zimr   r   r   r   Ú<module>   sZ   

D
)
	#%
