o
    Ԧ/f$                     @  sH  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d6d7ddZd8d9ddZG dd dZG dd deZejdkrRee G dd deZejdkrce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red+ree ed,ree ed-ree ed.ree ed/ree G d0d1 d1eZzd d2lmZ W n	 ey   Y nw ee e d3kr"e!ej"d4k re#d5 e$  e%ej"d Z&e#ee&gej"d4d R   W d   dS 1 sw   Y  dS dS ):    )annotationsNquote)Any   )ImageorderintreturnNonec                 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::

        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   -lib/python3.10/site-packages/PIL/ImageShow.pyregister   s   
r   imageImage.Imagetitle
str | Noneoptionsr   boolc                 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                   @  sj   e Zd ZU dZddd	Zd
Zded< 	 i Zded< 	 dddZdddZ	dddZ
dddZdddZd
S ) r   zBase class for viewers.r   r   r   r   r
   r	   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getmodebaseZconvert
show_image)selfr   r   baser   r   r   r   J   s   


zViewer.showNr   r    zdict[str, Any]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filestrc                 K  s   d}t |)zj
        Returns the command used to display the file.
        Not implemented in the base class.
        zunavailable in base viewer)NotImplementedError)r"   r&   r   msgr   r   r   get_commande   s   zViewer.get_commandc                 C  s   |j dd| |i| jS )z+Save to temporary file and return filename.r    Nr   )Z_dumpr%   r   r$   r   r   r   
save_imagem   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!   q   s   zViewer.show_imagepathc                 K  s   t | j|fi | dS )%
        Display given file.
        r   )ossystemr*   r"   r.   r   r   r   r   r,   u   s   zViewer.show_filer   r   r   r   r
   r	   )r   r   r
   r   r&   r'   r   r   r
   r'   )r   r   r
   r'   r.   r'   r   r   r
   r	   )__name__
__module____qualname____doc__r   r    __annotations__r   r%   r*   r+   r!   r,   r   r   r   r   r   E   s   
 




r   c                   @  2   e Zd ZdZdZdddZdddZ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   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.   r	   c                 K  s(   t j| j|fi |dtt dd dS )r/   TZCREATE_NO_WINDOW)shellZcreationflagsr   )
subprocessPopenr*   getattrr2   r   r   r   r,      s   zWindowsViewer.show_fileNr4   r5   r6   r7   r8   r9   r    r   r*   r,   r   r   r   r   r<          

r<   win32c                   @  r;   )	MacViewerz2The default viewer on macOS using ``Preview.app``.r   r   Tr=   r&   r'   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.   r	   c                 K  s<   t ddd|g tjptd}|rt |dd|g dS )r/   openz-azPreview.appZpython3z-cz<import os, sys, time; time.sleep(20); os.remove(sys.argv[1])r   )rA   Zcallsys
executableshutilwhichrB   )r"   r.   r   rN   r   r   r   r,      s   zMacViewer.show_fileNr4   r5   rD   r   r   r   r   rG      rE   rG   darwinc                   @  s4   e Zd ZdZdddZejdddZdddZdS )
UnixViewerr   r   Tr=   r&   r'   r   r   r
   tuple[str, str]c                 K  s   d S Nr   r?   r   r   r   get_command_ex   s   zUnixViewer.get_command_exc                 K  s*   | j |fi |d }d| dt| S )Nr   rH   rI   )rU   r   rJ   r   r   r   r*      s   zUnixViewer.get_commandNr&   r'   r   r   r
   rS   r4   )	r6   r7   r8   r    r   abcabstractmethodrU   r*   r   r   r   r   rR      s    
rR   c                   @  $   e Zd ZdZddd	ZdddZdS )	XDGViewerz3
    The freedesktop.org ``xdg-open`` command.
    r&   r'   r   r   r
   rS   c                 K  s   d }}||fS )Nxdg-openr   )r"   r&   r   rK   rN   r   r   r   rU      s   zXDGViewer.get_command_exr.   r	   c                 K  s   t d|g dS )r/   r[   r   rA   rB   r2   r   r   r   r,      s   zXDGViewer.show_fileNrV   r5   r6   r7   r8   r9   rU   r,   r   r   r   r   rZ      s    
rZ   c                   @  (   e Zd ZdZ	ddddZdddZdS )DisplayViewerz`
    The ImageMagick ``display`` command.
    This viewer supports the ``title`` parameter.
    Nr&   r'   r   r   r   r   r
   rS   c                 K  &   d }}|r|dt | 7 }||fS )Ndisplayz -title r   r"   r&   r   r   rK   rN   r   r   r   rU      s   zDisplayViewer.get_command_exr.   r	   c                 K  8   dg}| d}|r|d|g7 }|| t| dS )r/   ra   r   z-titler   getr   rA   rB   r"   r.   r   argsr   r   r   r   r,         


zDisplayViewer.show_filerT   r&   r'   r   r   r   r   r
   rS   r5   r]   r   r   r   r   r_      s
    r_   c                   @  rY   )GmDisplayViewerz*The GraphicsMagick ``gm display`` command.r&   r'   r   r   r
   rS   c                 K     d}d}||fS )Ngmz
gm displayr   r"   r&   r   rN   rK   r   r   r   rU         zGmDisplayViewer.get_command_exr.   r	   c                 K     t dd|g dS )r/   rl   ra   r   r\   r2   r   r   r   r,        zGmDisplayViewer.show_fileNrV   r5   r]   r   r   r   r   rj          
rj   c                   @  rY   )	EogViewerz'The GNOME Image Viewer ``eog`` command.r&   r'   r   r   r
   rS   c                 K  rk   )Neogzeog -nr   rm   r   r   r   rU     rn   zEogViewer.get_command_exr.   r	   c                 K  ro   )r/   rs   z-nr   r\   r2   r   r   r   r,     rp   zEogViewer.show_fileNrV   r5   r]   r   r   r   r   rr   
  rq   rr   c                   @  r^   )XVViewerzX
    The X Viewer ``xv`` command.
    This viewer supports the ``title`` parameter.
    Nr&   r'   r   r   r   r   r
   rS   c                 K  r`   )Nxvz -name r   rb   r   r   r   rU      s   zXVViewer.get_command_exr.   r	   c                 K  rc   )r/   ru   r   z-namer   rd   rf   r   r   r   r,   *  rh   zXVViewer.show_filerT   ri   r5   r]   r   r   r   r   rt     s
    
rt   )rF   rQ   r[   ra   rl   rs   ru   c                   @  s   e Zd ZdZddd	Zd
S )IPythonViewerz!The viewer for IPython frontends.r   r   r   r   r
   r	   c                 K  s   t | dS )Nr   )ipython_displayr-   r   r   r   r!   H  s   zIPythonViewer.show_imageNr3   )r6   r7   r8   r9   r!   r   r   r   r   rv   E  s    rv   )ra   __main__   z.Syntax: python3 ImageShow.py imagefile [title])r   )r   r	   r
   r   rT   )r   r   r   r   r   r   r
   r   )'Z
__future__r   rW   r0   rO   rA   rM   Zshlexr   typingr    r   r   r   r   r   r<   platformrG   rR   rZ   r_   rj   rr   rt   rP   rv   ZIPython.displayra   rw   ImportErrorr6   lenargvprintexitrL   Zimr   r   r   r   <module>   sf   ;








 $