U
    Evf$                  	   @  sL  d dl mZ 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
 ddlmZ g Zd3ddd	d
dZd4dddddddZG dd dZG dd deZejdkree G dd deZejdkre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'kred(rVee ed)rjee ed*r~ee ed+ree ed,ree G d-d. d.eZzd d/lmZ W n ek
r   Y n
X ee e d0krHe!ej"d1k re#d2 e$  e%ej"d "Z&e#ee&fej"d1d   W 5 Q R X dS )5    )annotationsNquote)Any   )ImageintNone)orderreturnc                 C  sN   z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::

        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)viewerr
    r   1/tmp/pip-unpacked-wheel-ciywl2yd/PIL/ImageShow.pyregister   s    

r   Image.Image
str | Noner   bool)imagetitleoptionsr   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.
    r   TF)r   show)r   r   r   r   r   r   r   r   6   s    	r   c                   @  s   e Zd ZU dZddddddZdZd	ed
< i Zded< dd	dddZddddddZ	dddddZ
ddddddZddddddZdS )r   zBase class for viewers.r   r   r   r   r   r   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   J   s    

zViewer.showNr   r!   zdict[str, Any]r   )r   r   c                 C  s   | j S )z3Return format name, or ``None`` to save as PGM/PPM.)r!   r$   r   r   r   r   
get_formata   s    zViewer.get_formatstrfiler   r   c                 K  s   d}t |dS )zj
        Returns the command used to display the file.
        Not implemented in the base class.
        zunavailable in base viewerN)NotImplementedError)r$   r*   r   msgr   r   r   get_commande   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_imagem   s    zViewer.save_imagec                 K  s   | j | |f|S )zDisplay the given image.)	show_filer/   r$   r   r   r   r   r   r#   q   s    zViewer.show_imagepathr   r   c                 K  s   t | j|f| dS )%
        Display given file.
        r   )ossystemr-   r$   r3   r   r   r   r   r0   u   s    zViewer.show_file)__name__
__module____qualname____doc__r   r!   __annotations__r   r'   r-   r/   r#   r0   r   r   r   r   r   E   s   
r   c                   @  sB   e Zd ZdZdZdddZddddd	d
Zdddd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_allr(   r   r)   c                 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   r   r-      s    zWindowsViewer.get_commandr   r2   c                 K  s$   t j| j|f|dtt dd dS )r4   TZCREATE_NO_WINDOW)shellcreationflagsr   )
subprocessPopenr-   getattrr7   r   r   r   r0      s    zWindowsViewer.show_fileNr8   r9   r:   r;   r!   r   r-   r0   r   r   r   r   r=      s
   
r=   win32c                   @  sB   e Zd ZdZdZdddZddddd	d
ZddddddZdS )	MacViewerz2The default viewer on macOS using ``Preview.app``.r   r   Tr>   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_commandr   r2   c                 K  s<   t ddd|g tjp td}|r8t |dd|g dS )r4   openz-azPreview.apppython3z-cz<import os, sys, time; time.sleep(20); os.remove(sys.argv[1])r   )rC   callsys
executableshutilwhichrD   )r$   r3   r   rQ   r   r   r   r0      s    zMacViewer.show_fileNrF   r   r   r   r   rH      s
   
rH   darwinc                   @  sD   e Zd ZdZdddZejddddd	d
ZddddddZdS )
UnixViewerr   r   Tr>   r(   r   tuple[str, str]r)   c                 K  s   d S )Nr   r@   r   r   r   get_command_ex   s    zUnixViewer.get_command_exc                 K  s&   | j |f|d }d| dt| S )Nr   rI   rJ   )rW   r   rK   r   r   r   r-      s    zUnixViewer.get_commandN)	r8   r9   r:   r!   r   abcabstractmethodrW   r-   r   r   r   r   rU      s
   
rU   c                   @  s4   e Zd ZdZddddddZdddd	d
dZdS )	XDGViewerz3
    The freedesktop.org ``xdg-open`` command.
    r(   r   rV   r)   c                 K  s   d }}||fS )Nxdg-openr   )r$   r*   r   rL   rQ   r   r   r   rW      s    zXDGViewer.get_command_exr   r2   c                 K  s   t d|g dS )r4   r[   r   rC   rD   r7   r   r   r   r0      s    zXDGViewer.show_fileNr8   r9   r:   r;   rW   r0   r   r   r   r   rZ      s   rZ   c                   @  s8   e Zd ZdZdddddddd	Zddd
dddZdS )DisplayViewerz`
    The ImageMagick ``display`` command.
    This viewer supports the ``title`` parameter.
    Nr(   r   r   rV   r*   r   r   r   c                 K  s&   d }}|r|dt | 7 }||fS )Ndisplayz -title r   r$   r*   r   r   rL   rQ   r   r   r   rW      s    zDisplayViewer.get_command_exr   r2   c                 K  s8   dg}| d}|r |d|g7 }|| t| dS )r4   r`   r   z-titler   getr   rC   rD   r$   r3   r   argsr   r   r   r   r0      s    


zDisplayViewer.show_file)Nr]   r   r   r   r   r^      s    r^   c                   @  s4   e Zd ZdZddddddZdddd	d
dZdS )GmDisplayViewerz*The GraphicsMagick ``gm display`` command.r(   r   rV   r)   c                 K  s   d}d}||fS )Ngmz
gm displayr   r$   r*   r   rQ   rL   r   r   r   rW      s    zGmDisplayViewer.get_command_exr   r2   c                 K  s   t dd|g dS )r4   rg   r`   r   r\   r7   r   r   r   r0     s    zGmDisplayViewer.show_fileNr]   r   r   r   r   rf      s   rf   c                   @  s4   e Zd ZdZddddddZdddd	d
dZdS )	EogViewerz'The GNOME Image Viewer ``eog`` command.r(   r   rV   r)   c                 K  s   d}d}||fS )Neogzeog -nr   rh   r   r   r   rW     s    zEogViewer.get_command_exr   r2   c                 K  s   t dd|g dS )r4   rj   z-nr   r\   r7   r   r   r   r0     s    zEogViewer.show_fileNr]   r   r   r   r   ri   
  s   ri   c                   @  s8   e Zd ZdZdddddddd	Zddd
dddZdS )XVViewerzX
    The X Viewer ``xv`` command.
    This viewer supports the ``title`` parameter.
    Nr(   r   r   rV   r_   c                 K  s&   d }}|r|dt | 7 }||fS )Nxvz -name r   ra   r   r   r   rW      s    zXVViewer.get_command_exr   r2   c                 K  s8   dg}| d}|r |d|g7 }|| t| dS )r4   rl   r   z-namer   rb   rd   r   r   r   r0   *  s    


zXVViewer.show_file)Nr]   r   r   r   r   rk     s    
rk   )rG   rT   r[   r`   rg   rj   rl   c                   @  s"   e Zd ZdZddddddZdS )	IPythonViewerz!The viewer for IPython frontends.r   r   r   r   c                 K  s   t | dS )Nr   )ipython_displayr1   r   r   r   r#   H  s    zIPythonViewer.show_imageN)r8   r9   r:   r;   r#   r   r   r   r   rm   E  s   rm   )r`   __main__   z.Syntax: python3 ImageShow.py imagefile [title])r   )N)'
__future__r   rX   r5   rR   rC   rP   shlexr   typingr    r   r   r   r   r   r=   platformrH   rU   rZ   r^   rf   ri   rk   rS   rm   ZIPython.displayr`   rn   ImportErrorr8   lenargvprintexitrM   Zimr   r   r   r   <module>   s^   ;


