a
    Cg#                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlZd dlmZ d dlmZ dd	lmZ G d
d deZG dd deZG dd d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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!G d&d' d'eZ"G d(d) d)eZ#G d*d+ d+eZ$e% j&ee' j&ee( j&ee) j&ee* j&ee+ j&e d,e!e, j&e"e- j&e#e. j&e$i
Z/dS )-    N)BytesIO)copyfileobj)NamedTemporaryFileSpooledTemporaryFile)Optional)ElementTree)image   )registryc                   @   s   e Zd ZdS )UnrecognisedImageFormatErrorN)__name__
__module____qualname__ r   r   K/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/willow/image.pyr      s   r   c                   @   s   e Zd ZdZdS )BadImageOperationErrorz
    Raised when the arguments to an image operation are invalid,
    e.g. a crop where the left coordinate is greater than the right coordinate
    N)r   r   r   __doc__r   r   r   r   r      s   r   c                   @   s   e Zd Zedd Zedd ZedddZeddd	Zd
d Z	edd Z
edd Zded dddZedddZdS )Imagec                 C   s   d S Nr   )clsr   r   r   check   s    zImage.checkc                 C   s
   d| _ | S NT)Z_willow_operationfuncr   r   r   	operation!   s    zImage.operationNc                    s    fdd}|S )Nc                    s    f| _ | S r   )Z_willow_converter_tor   costto_classr   r   wrapper(   s    
z#Image.converter_to.<locals>.wrapperr   )r   r   r   r   r   r   converter_to&   s    zImage.converter_toc                    s    fdd}|S )Nc                    sJ   t | dsg | _ttr6| j fddD  n| j f | S )N_willow_converter_fromc                    s   g | ]}| fqS r   r   ).0Zsc)r   r   r   
<listcomp>5       z9Image.converter_from.<locals>.wrapper.<locals>.<listcomp>)hasattrr    
isinstancelistextendappendr   r   
from_classr   r   r   0   s    

z%Image.converter_from.<locals>.wrapperr   )r*   r   r   r   r)   r   converter_from.   s    zImage.converter_fromc                    sZ   zt t|\} }W n( tyD   tjjd|Y n0  fdd}|S )Nz object has no attribute c                     s0   } D ]\}}||}q|g| R i |S r   r   )argskwargsr   	converter_Zconversion_pathr   selfr   r   r   F   s    
z"Image.__getattr__.<locals>.wrapper)r
   Zfind_operationtypeLookupErrorAttributeError	__class__r   )r1   attrr/   r   r   r0   r   __getattr__=   s    
zImage.__getattr__c                 C   sZ   t |}|d u r | |r d}t|}|sR|rJtd| dtdntd||S )NsvgzCannot load z	 images ()zUnknown image format)filetypeguess_extension	maybe_xmlINITIAL_IMAGE_CLASSESgetr   )r   fimage_formatZinitial_classr   r   r   openR   s    

z
Image.openc                 C   sF   | d td}|D ]}||r| d  dS q| d dS )Nr   s   ^\s*<TF)seekrecompilematch)r   r?   patternliner   r   r   r<   f   s    




zImage.maybe_xmlT	ImageFile)returnc                 C   s0   |dvrt d| d| }t| |||dS )N)
jpegpnggifbmptiffwebpr8   heicavificozUnknown image format: Zsave_as_)apply_optimizers)
ValueErrorgetattr)r1   r@   outputrS   Zoperation_namer   r   r   saves   s    z
Image.save)r@   c           	   	   C   s  t |}|sdS d}z`t|ttfrrtdd.}d}|d t|| |j}W d   q1 sf0    Y  nt	|dr|j}nnt|t
r|}n^t|trtdd$}|| |j}d}W d   q1 s0    Y  ntdt| d|D ]}|| qt	|d	rR|d t|d
}t|| W d   n1 sH0    Y  t	|drf|  W |rt| n|rt| 0 dS )a  
        Runs all available optimizers for the given image format on the given image file.

        If the passed image file is a SpooledTemporaryFile or just bytes, we are converting it to a
        NamedTemporaryFile to guarantee we can access the file so the optimizers to work on it.
        If we get a string, we assume it's a path to a file, and will attempt to load it from
        the file system.
        NF)deleteTr   namezCannot optimise z3. It must be a readable object, or a path to a filerB   rbtruncate)r
   Zget_optimizers_for_formatr%   r   r   r   rB   r   rY   r$   strbyteswrite	TypeErrorr2   processrA   r[   osunlink)	r1   Z
image_filer@   Z
optimizersZnamed_file_createdZ
named_file	file_pathZ	optimizerr?   r   r   r   optimize   sF    	


&



$
*
zImage.optimize)N)N)T)r   r   r   classmethodr   staticmethodr   r   r+   r7   rA   r<   r   rW   r\   rd   r   r   r   r   r      s$   



 r   c                   @   s"   e Zd Zdd Zejdd ZdS )ImageBufferc                 C   s   || _ || _d S r   )sizedata)r1   rh   ri   r   r   r   __init__   s    zImageBuffer.__init__c                 C   s   | j S r   )rh   r1   r   r   r   get_size   s    zImageBuffer.get_sizeN)r   r   r   rj   r   r   rl   r   r   r   r   rg      s   rg   c                   @   s,   e Zd ZdZejdd Zejdd ZdS )RGBImageBufferZRGBc                 C   s   dS NFr   rk   r   r   r   	has_alpha   s    zRGBImageBuffer.has_alphac                 C   s   dS rn   r   rk   r   r   r   has_animation   s    zRGBImageBuffer.has_animationNr   r   r   moder   r   ro   rp   r   r   r   r   rm      s
   
rm   c                   @   s,   e Zd ZdZejdd Zejdd ZdS )RGBAImageBufferZRGBAc                 C   s   dS r   r   rk   r   r   r   ro      s    zRGBAImageBuffer.has_alphac                 C   s   dS rn   r   rk   r   r   r   rp      s    zRGBAImageBuffer.has_animationNrq   r   r   r   r   rs      s
   
rs   c                   @   s,   e Zd Zedd Zedd Zdd ZdS )rH   c                 C   s   t dS )zq
        Willow internal name for the image format
        ImageFile implementations MUST override this.
        NNotImplementedErrorrk   r   r   r   format_name   s    zImageFile.format_namec                 C   s   t dS )zo
        Returns the MIME type of the image file
        ImageFile implementations MUST override this.
        Nrt   rk   r   r   r   	mime_type   s    zImageFile.mime_typec                 C   s
   || _ d S r   )r?   )r1   r?   r   r   r   rj      s    zImageFile.__init__N)r   r   r   propertyrv   rw   rj   r   r   r   r   rH      s
   

rH   c                   @   s$   e Zd Zedd Zedd ZdS )JPEGImageFilec                 C   s   dS )NrJ   r   rk   r   r   r   rv      s    zJPEGImageFile.format_namec                 C   s   dS )Nz
image/jpegr   rk   r   r   r   rw      s    zJPEGImageFile.mime_typeNr   r   r   rx   rv   rw   r   r   r   r   ry      s   
ry   c                   @   s$   e Zd Zedd Zedd ZdS )PNGImageFilec                 C   s   dS )NrK   r   rk   r   r   r   rv     s    zPNGImageFile.format_namec                 C   s   dS )Nz	image/pngr   rk   r   r   r   rw     s    zPNGImageFile.mime_typeNrz   r   r   r   r   r{     s   
r{   c                   @   s$   e Zd Zedd Zedd ZdS )GIFImageFilec                 C   s   dS )NrL   r   rk   r   r   r   rv     s    zGIFImageFile.format_namec                 C   s   dS )Nz	image/gifr   rk   r   r   r   rw     s    zGIFImageFile.mime_typeNrz   r   r   r   r   r|     s   
r|   c                   @   s$   e Zd Zedd Zedd ZdS )BMPImageFilec                 C   s   dS )NrM   r   rk   r   r   r   rv     s    zBMPImageFile.format_namec                 C   s   dS )Nz	image/bmpr   rk   r   r   r   rw     s    zBMPImageFile.mime_typeNrz   r   r   r   r   r}     s   
r}   c                   @   s$   e Zd Zedd Zedd ZdS )TIFFImageFilec                 C   s   dS )NrN   r   rk   r   r   r   rv   "  s    zTIFFImageFile.format_namec                 C   s   dS )Nz
image/tiffr   rk   r   r   r   rw   &  s    zTIFFImageFile.mime_typeNrz   r   r   r   r   r~   !  s   
r~   c                   @   s$   e Zd Zedd Zedd ZdS )WebPImageFilec                 C   s   dS )NrO   r   rk   r   r   r   rv   ,  s    zWebPImageFile.format_namec                 C   s   dS )Nz
image/webpr   rk   r   r   r   rw   0  s    zWebPImageFile.mime_typeNrz   r   r   r   r   r   +  s   
r   c                       s&   e Zd ZdZdZd fdd	Z  ZS )SvgImageFiler8   zimage/svg+xmlNc                    s@   |d u r*| d t|| _| d n|| _t | d S )Nr   )rB   r   parsedomsuperrj   )r1   r?   r   r5   r   r   rj   9  s    
zSvgImageFile.__init__)N)r   r   r   rv   rw   rj   __classcell__r   r   r   r   r   5  s   r   c                   @   s$   e Zd Zedd Zedd ZdS )HeicImageFilec                 C   s   dS )NrP   r   rk   r   r   r   rv   E  s    zHeicImageFile.format_namec                 C   s   dS )Nz
image/heicr   rk   r   r   r   rw   I  s    zHeicImageFile.mime_typeNrz   r   r   r   r   r   D  s   
r   c                   @   s$   e Zd Zedd Zedd ZdS )AvifImageFilec                 C   s   dS )NrQ   r   rk   r   r   r   rv   O  s    zAvifImageFile.format_namec                 C   s   dS )Nz
image/avifr   rk   r   r   r   rw   S  s    zAvifImageFile.mime_typeNrz   r   r   r   r   r   N  s   
r   c                   @   s   e Zd ZdZdZdS )IcoImageFilerR   zimage/x-iconN)r   r   r   rv   rw   r   r   r   r   r   X  s   r   r8   )0ra   rC   ior   shutilr   tempfiler   r   typingr   r:   Z
defusedxmlr   Zfiletype.typesr   Zimage_typesr
   IOErrorr   rT   r   r   rg   rm   rs   rH   ry   r{   r|   r}   r~   r   r   r   r   r   ZJpeg	extensionZPngZGifZBmpZTiffZWebpZHeicZAvifZIcor=   r   r   r   r   <module>   sL   	 '

















