
    e               	         d dl m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Zd)dZ G d d	      Z G d
 de      Zej                  dk(  r ee        G d de      Zej                  dk(  r ee        G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zej                  dvr ej.                  d      r ee        ej.                  d      r ee        ej.                  d      r ee        ej.                  d       r ee        ej.                  d!      r ee        G d" d#e      Z	 d d$lmZ  ee       ed%k(  rs eej>                        d&k  r e d'        ejB                           e	jD                  ej>                  d         5 Z# e  ee#gej>                  d&d         ddd       yy# e$ r Y w xY w# 1 sw Y   yxY w)*    )annotationsNquote   )Imagec                    	 t        | t              r |        } |dkD  rt        j	                  |        yt        j                  d|        y# t        $ r Y =w xY w)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orders     //usr/lib/python3/dist-packages/PIL/ImageShow.pyregisterr      sO    ff%XF qy6"  s   A 	AAc                H    t         D ]  } |j                  | fd|i|s y y)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   s       r   r   r   4   s3      6;;u5E5W5     c                  @    e Zd ZdZd ZdZ	 i Z	 d Zd Zd Z	d Z
d Zy)	r
   zBase class for viewers.c                    |j                   dv s\| j                  dk(  r|j                   dv s?t        j                  |j                         }|j                   |k7  r|j	                  |      } | 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   bases       r   r   zViewer.showH   sk     JJ-'u$~)E$$UZZ0DzzT!d+tu000r   Nc                    | j                   S )z3Return format name, or ``None`` to save as PGM/PPM.)r    r$   r   s     r   
get_formatzViewer.get_format_   s    {{r   c                    d}t        |      )zj
        Returns the command used to display the file.
        Not implemented in the base class.
        zunavailable in base viewer)NotImplementedError)r$   filer   msgs       r   get_commandzViewer.get_commandc   s    
 +!#&&r   c                \     |j                   dd| j                  |      i| j                  S )z+Save to temporary file and return filename.r     )_dumpr(   r   r'   s     r   
save_imagezViewer.save_imagek   s(    u{{I$//%"8IDLLIIr   c                F     | j                   | j                  |      fi |S )zDisplay the given image.)	show_filer1   r$   r   r   s      r   r#   zViewer.show_imageo   s!    t~~dooe4@@@r   c                P    t        j                   | j                  |fi |       y)%
        Display given file.
        r   )ossystemr-   r$   pathr   s      r   r3   zViewer.show_files   s&     			"$""43734r   )__name__
__module____qualname____doc__r   r    r   r(   r-   r1   r#   r3   r/   r   r   r
   r
   C   s7    !1$ F/G7'JAr   r
   c                  $    e Zd ZdZdZdddZd Zy)WindowsViewerzNThe default viewer on Windows is the default system application for PNG files.r   r   Tcompress_levelsave_allc                    d| d| dS )Nzstart "Pillow" /WAIT "z)" && ping -n 4 127.0.0.1 >NUL && del /f ""r/   )r$   r+   r   s      r   r-   zWindowsViewer.get_command   s    $TF +q"	
r   N)r;   r<   r=   r>   r    r   r-   r/   r   r   r@   r@   ~   s    XF!"5G
r   r@   win32c                  *    e Zd ZdZdZdddZd Zd Zy)		MacViewerz2The default viewer on macOS using ``Preview.app``.r   r   TrA   c                F    d}d| dt        |       dt        |       d}|S )Nzopen -a Preview.app( z; sleep 20; rm -f z)&r   r$   r+   r   commands       r   r-   zMacViewer.get_command   s1     (gYad},>uT{m2Nr   c                    t        j                  ddd|g       t        j                  xs t	        j
                  d      }|rt        j                  |dd|g       y)r6   openz-azPreview.apppython3z-cz<import os, sys, time; time.sleep(20); os.remove(sys.argv[1])r   )
subprocesscallsys
executableshutilwhichPopen)r$   r:   r   rT   s       r   r3   zMacViewer.show_file   sW     	}d;<^^>v||I'>
R	 r   N)r;   r<   r=   r>   r    r   r-   r3   r/   r   r   rH   rH      s    <F!"5Gr   rH   darwinc                       e Zd ZdZdddZd Zy)
UnixViewerr   r   TrA   c                P     | j                   |fi |d   }d| dt        |       S )Nr   rJ   rK   )get_command_exr   rL   s       r   r-   zUnixViewer.get_command   s5    %$%%d6g6q97)1U4[M**r   N)r;   r<   r=   r    r   r-   r/   r   r   rZ   rZ      s    F!"5G+r   rZ   c                      e Zd ZdZd Zd Zy)	XDGViewerz3
    The freedesktop.org ``xdg-open`` command.
    c                    dx}}||fS )Nxdg-openr/   )r$   r+   r   rM   rT   s        r   r\   zXDGViewer.get_command_ex   s    ))*
""r   c                2    t        j                  d|g       y)r6   r`   r   rQ   rW   r9   s      r   r3   zXDGViewer.show_file   s     	*d+,r   Nr;   r<   r=   r>   r\   r3   r/   r   r   r^   r^      s    #r   r^   c                      e Zd ZdZddZd Zy)DisplayViewerz`
    The ImageMagick ``display`` command.
    This viewer supports the ``title`` parameter.
    Nc                8    dx}}|r|dt        |       z  }||fS )Ndisplayz -title r   r$   r+   r   r   rM   rT   s         r   r\   zDisplayViewer.get_command_ex   s/    ((*%,00G
""r   c                    dg}|j                  d      }|r|d|gz  }|j                  |       t        j                  |       y)r6   rg   r   z-titler   getr   rQ   rW   r$   r:   r   argsr   s        r   r3   zDisplayViewer.show_file   sH     {G$Xu%%DDr   Nrc   r/   r   r   re   re      s    
#r   re   c                      e Zd ZdZd Zd Zy)GmDisplayViewerz*The GraphicsMagick ``gm display`` command.c                    d}d}||fS )Ngmz
gm displayr/   r$   r+   r   rT   rM   s        r   r\   zGmDisplayViewer.get_command_ex   s    

""r   c                4    t        j                  dd|g       y)r6   rr   rg   r   rb   r9   s      r   r3   zGmDisplayViewer.show_file   s     	$	401r   Nrc   r/   r   r   rp   rp      s    4#
r   rp   c                      e Zd ZdZd Zd Zy)	EogViewerz'The GNOME Image Viewer ``eog`` command.c                    d}d}||fS )Neogzeog -nr/   rs   s        r   r\   zEogViewer.get_command_ex   s    

""r   c                4    t        j                  dd|g       y)r6   rx   z-nr   rb   r9   s      r   r3   zEogViewer.show_file   s     	%t,-r   Nrc   r/   r   r   rv   rv      s    1#
r   rv   c                      e Zd ZdZddZd Zy)XVViewerzX
    The X Viewer ``xv`` command.
    This viewer supports the ``title`` parameter.
    Nc                8    dx}}|r|dt        |       z  }||fS )Nxvz -name r   rh   s         r   r\   zXVViewer.get_command_ex  s1      $#*u//G
""r   c                    dg}|j                  d      }|r|d|gz  }|j                  |       t        j                  |       y)r6   r}   r   z-namer   rj   rl   s        r   r3   zXVViewer.show_file  sH     vG$We$$DDr   rn   rc   r/   r   r   r{   r{     s    
#r   r{   )rF   rX   r`   rg   rr   rx   r}   c                      e Zd ZdZd Zy)IPythonViewerz!The viewer for IPython frontends.c                    t        |       y)Nr   )ipython_displayr4   s      r   r#   zIPythonViewer.show_image3  s    r   N)r;   r<   r=   r>   r#   r/   r   r   r   r   0  s
    +r   r   )rg   __main__   z.Syntax: python3 ImageShow.py imagefile [title])r   rn   )$
__future__r   r7   rU   rQ   rS   shlexr    r   r   r   r   r
   r@   platformrH   rZ   r^   re   rp   rv   r{   rV   r   IPython.displayrg   r   ImportErrorr;   lenargvprintexitrO   imr/   r   r   <module>r      s   # 	   
  #25 5v
F 
 <<7] > <<8Y+ +
 "J 4j  
  z 8 <<**v||Jv||Iv||D!v||Ev||DF : ] z
388}q>?
	CHHQK	  'Bd2%%&' '   		' 's   G  9G+ G('G(+G4