a
    Ag	'                     @  sd  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d4ddd	d
ddZd5d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(vred)rXee ed*rlee ed+ree ed,ree ed-ree G d.d/ d/eZzd d0lmZ W n ey   Y n
0 ee e d1kr`e!ej"d2k re#d3 e$  e%ej"d .Z&e#ee&gej"d2d R   W d   n1 sV0    Y  dS )6    )annotationsNquote)Any   )Imageztype[Viewer] | ViewerintNone)viewerorderreturnc                 C  s>   t | trt| tr|  } |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)
isinstancetype
issubclassViewer_viewersappendinsert)r
   r    r   L/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/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   3   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  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   G   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_format^   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_commandb   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_filer0   r%   r   r   r   r   r   r$   n   s    zViewer.show_imagepathr   r   c                 K  s,   t j|stt | j|fi | dS )%
        Display given file.
        r   )osr4   existsFileNotFoundErrorsystemr.   r%   r4   r   r   r   r   r1   r   s    zViewer.show_file)__name__
__module____qualname____doc__r   r"   __annotations__r   r(   r.   r0   r$   r1   r   r   r   r   r   B   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   r3   c                 K  s8   t j|sttj| j|fi |dttdd dS )r5   TZCREATE_NO_WINDOW)shellcreationflagsr   )r6   r4   r7   r8   
subprocessPopenr.   getattrr:   r   r   r   r1      s    zWindowsViewer.show_fileNr;   r<   r=   r>   r"   r   r.   r1   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   TrA   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   r3   c                 K  sL   t j|sttddd|g tjp0t	d}|rHt
|dd|g dS )r5   openz-azPreview.apppython3z-cz<import os, sys, time; time.sleep(20); os.remove(sys.argv[1])r   )r6   r4   r7   r8   rF   callsys
executableshutilwhichrG   )r%   r4   r   rT   r   r   r   r1      s    zMacViewer.show_fileNrI   r   r   r   r   rK      s
   
rK   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   TrA   r)   r   tuple[str, str]r*   c                 K  s   d S )Nr   rC   r   r   r   get_command_ex   s    zUnixViewer.get_command_exc                 K  s(   | j |fi |d }| dt| S )Nr   rM   )rZ   r   rN   r   r   r   r.      s    zUnixViewer.get_commandN)	r;   r<   r=   r"   r   abcabstractmethodrZ   r.   r   r   r   r   rX      s
   
rX   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   rY   r*   c                 K  s   d }}||fS )Nxdg-openr   )r%   r+   r   rO   rT   r   r   r   rZ      s    zXDGViewer.get_command_exr   r3   c                 K  s"   t j|sttd|g dS )r5   r^   r   r6   r4   r7   r8   rF   rG   r:   r   r   r   r1      s    zXDGViewer.show_fileNr;   r<   r=   r>   rZ   r1   r   r   r   r   r]      s   r]   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   rY   r+   r   r   r   c                 K  s&   d }}|r|dt | 7 }||fS )Ndisplayz -title r   r%   r+   r   r   rO   rT   r   r   r   rZ      s    zDisplayViewer.get_command_exr   r3   c                 K  sH   t j|stdg}|d}|r0|d|g7 }|| t| dS )r5   rc   r   z-titler   r6   r4   r7   r8   getr   rF   rG   r%   r4   r   argsr   r   r   r   r1      s    


zDisplayViewer.show_file)Nr`   r   r   r   r   ra      s    ra   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   rY   r*   c                 K  s   d}d}||fS )Ngmz
gm displayr   r%   r+   r   rT   rO   r   r   r   rZ     s    zGmDisplayViewer.get_command_exr   r3   c                 K  s$   t j|sttdd|g dS )r5   rj   rc   r   r_   r:   r   r   r   r1   	  s    zGmDisplayViewer.show_fileNr`   r   r   r   r   ri     s   ri   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   rY   r*   c                 K  s   d}d}||fS )Neogzeog -nr   rk   r   r   r   rZ     s    zEogViewer.get_command_exr   r3   c                 K  s$   t j|sttdd|g dS )r5   rm   z-nr   r_   r:   r   r   r   r1     s    zEogViewer.show_fileNr`   r   r   r   r   rl     s   rl   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   rY   rb   c                 K  s&   d }}|r|dt | 7 }||fS )Nxvz -name r   rd   r   r   r   rZ   +  s    zXVViewer.get_command_exr   r3   c                 K  sH   t j|stdg}|d}|r0|d|g7 }|| t| dS )r5   ro   r   z-namer   re   rg   r   r   r   r1   5  s    


zXVViewer.show_file)Nr`   r   r   r   r   rn   %  s    
rn   )rJ   rW   r^   rc   rj   rm   ro   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_displayr2   r   r   r   r$   U  s    zIPythonViewer.show_imageN)r;   r<   r=   r>   r$   r   r   r   r   rp   R  s   rp   )rc   __main__   z.Syntax: python3 ImageShow.py imagefile [title])r   )N)'
__future__r   r[   r6   rU   rF   rS   shlexr   typingr    r   r   r   r   r   r@   platformrK   rX   r]   ra   ri   rl   rn   rV   rp   ZIPython.displayrc   rq   ImportErrorr;   lenargvprintexitrP   Zimr   r   r   r   <module>   s^   =
!
 
