B
    ñ¶I`—  ã            	   @   s˜  d dl Z 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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ejdk�r2e d¡�r
e
eƒ e d¡�re
eƒ e d¡�r2e
eƒ edk�r”eejƒdk �r\edƒ e ¡  e ejd ¡�"Zeeefejdd… žŽ ƒ W dQ R X dS )"é    N)Úquote)ÚImageé   c             C   sN   yt | tƒr| ƒ } W n tk
r(   Y nX |dkr>t | ¡ n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   ú1/tmp/pip-unpacked-wheel-wndzrte2/PIL/ImageShow.pyÚregister   s    	

r   c             K   s,   x&t D ]}|j| fd|i|—ŽrdS qW 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               @   sH   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S )r   zBase class for viewers.c             K   sL   |j dks>| jdkr|j dks>t |j ¡}|j |kr>| |¡}| j|f|Ž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   B   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_formatZ   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_command^   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   r   Ú
save_imagee   s    zViewer.save_imagec             K   s   | j |  |¡f|ŽS )zDisplay the given image.)Ú	show_filer!   )r   r   r   r   r   r   r   i   s    zViewer.show_imagec             K   s   t  | j|f|Ž¡ dS )zDisplay the given file.r   )ÚosÚsystemr   )r   r   r   r   r   r   r"   m   s    zViewer.show_file)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r!   r   r"   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   r   r   |   s    zWindowsViewer.get_commandN)r%   r&   r'   r(   r   r   r   r   r   r   r   r)   v   s   r)   Ú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   r*   r   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_commandc          	   K   sb   t  ¡ \}}t |d¡�}| |¡ W dQ R X t|ƒ�}tjdgd|d� W dQ R X t |¡ dS )zDisplay given fileÚwNz7im=$(cat); open -a Preview.app $im; sleep 20; rm -f $imT)ÚshellÚstdinr   )	ÚtempfileÚmkstempr#   ÚfdopenÚwriteÚopenÚ
subprocessÚPopenÚremove)r   r   r   ÚfdÚpathÚfr   r   r   r"   •   s    

zMacViewer.show_fileN)r%   r&   r'   r(   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d„ ZdS )	Ú
UnixViewerr   r*   r   c             K   s2   | j |f|Žd }d|› dt|ƒ› dt|ƒ› d�S )Nr   r.   r/   z; rm -f z)&)Úget_command_exr   )r   r   r   r0   r   r   r   r   ¬   s    zUnixViewer.get_commandc          	   K   s|   t  ¡ \}}t |d¡�}| |¡ W dQ R X t|ƒ�2}| j|f|Žd }tjd| d gd|d� W dQ R X t 	|¡ dS )	zDisplay given filer1   Nr   z
im=$(cat);z $im; rm -f $imT)r2   r3   r   )
r4   r5   r#   r6   r7   r8   rA   r9   r:   r;   )r   r   r   r<   r=   r>   r0   r   r   r   r"   °   s    
 
zUnixViewer.show_fileN)r%   r&   r'   r   r   r   r"   r   r   r   r   r@   ¨   s   r@   c               @   s   e Zd ZdZdd„ ZdS )ÚDisplayViewerz$The ImageMagick ``display`` command.c             K   s   d }}||fS )NÚdisplayr   )r   r   r   r0   Ú
executabler   r   r   rA   Á   s    zDisplayViewer.get_command_exN)r%   r&   r'   r(   rA   r   r   r   r   rB   ¾   s   rB   c               @   s   e Zd ZdZdd„ ZdS )Ú	EogViewerz'The GNOME Image Viewer ``eog`` command.c             K   s   d }}||fS )NÚeogr   )r   r   r   r0   rD   r   r   r   rA   É   s    zEogViewer.get_command_exN)r%   r&   r'   r(   rA   r   r   r   r   rE   Æ   s   rE   c               @   s   e Zd Z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   )r   r   r   r   r0   rD   r   r   r   rA   Ô   s    zXVViewer.get_command_ex)N)r%   r&   r'   r(   rA   r   r   r   r   rG   Î   s   rG   )r,   r?   rC   rF   rH   Ú__main__é   z-Syntax: python ImageShow.py imagefile [title])r   )N)r#   Úshutilr9   Úsysr4   Úshlexr   ZPILr   r   r   r   r   r)   Úplatformr-   r@   rB   rE   rG   Úwhichr%   ÚlenÚargvÚprintÚexitr8   Zimr   r   r   r   Ú<module>   sB   

9


