U
    wh$                     @   s  d dl Z d dl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)NamedTemporaryFileSpooledTemporaryFile)Optional)ElementTree)image   )registryc                   @   s   e Zd ZdS )UnrecognisedImageFormatErrorN)__name__
__module____qualname__ r   r   T/var/www/dating/data/www/fatepal.com/env/lib/python3.8/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   ).0sc)r   r   r   
<listcomp>4   s     z9Image.converter_from.<locals>.wrapper.<locals>.<listcomp>)hasattrr   
isinstancelistextendappendr   r   
from_classr   r   r   /   s    

z%Image.converter_from.<locals>.wrapperr   )r)   r   r   r   r(   r   converter_from-   s    zImage.converter_fromc                    s\   zt t|\} }W n* tk
rF   tjjd|Y nX  fdd}|S )Nz object has no attribute c                     s*   } D ]\}}||}q|f| |S r   r   )argskwargsr   	converter_Zconversion_pathr   selfr   r   r   E   s    
z"Image.__getattr__.<locals>.wrapper)r	   Zfind_operationtypeLookupErrorAttributeError	__class__r   )r0   attrr.   r   r   r/   r   __getattr__<   s    
zImage.__getattr__c                 C   sZ   t |}|d k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   openQ   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;   e   s    




zImage.maybe_xmlT	ImageFile)returnc                 C   s.   |dkrt d| d| }t| |||dS )N)
jpegpnggifbmptiffwebpr7   heicavificozUnknown image format: %sZsave_as_)apply_optimizers)
ValueErrorgetattr)r0   r?   outputrR   Zoperation_namer   r   r   saver   s    z
Image.save)r?   c           
   	   C   s  t |}|sdS d}z^t|tr~|j}tdd>}t|drP|	|
  n|d |	|  |j}W 5 Q R X d}nt|trtdd}|	|
  |j}W 5 Q R X d}nXt|dr|j}nFt|tr|}n6t|trtdd}|	| |j}d}W 5 Q R X |D ]}|| qt|drt|d t|d	}	|	|	  W 5 Q R X t|d
rt|  W 5 |rt| X 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)deletegetvaluer   TnamerA   rbtruncate)r	   Zget_optimizers_for_formatosunlinkr$   r   _filer   r#   writerX   rA   readrY   r   strbytesprocessr@   r[   )
r0   Z
image_filer?   Z
optimizersZnamed_file_created	file_pathfileZ
named_fileZ	optimizerr>   r   r   r   optimize   sL    	








zImage.optimize)N)N)T)r   r   r   classmethodr   staticmethodr   r   r*   r6   r@   r;   r   rV   ra   rf   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)r0   rj   rk   r   r   r   __init__   s    zImageBuffer.__init__c                 C   s   | j S r   )rj   r0   r   r   r   get_size   s    zImageBuffer.get_sizeN)r   r   r   rl   r   r   rn   r   r   r   r   ri      s   ri   c                   @   s,   e Zd ZdZejdd Zejdd ZdS )RGBImageBufferRGBc                 C   s   dS NFr   rm   r   r   r   	has_alpha   s    zRGBImageBuffer.has_alphac                 C   s   dS rq   r   rm   r   r   r   has_animation   s    zRGBImageBuffer.has_animationNr   r   r   moder   r   rr   rs   r   r   r   r   ro      s
   
ro   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   rm   r   r   r   rr      s    zRGBAImageBuffer.has_alphac                 C   s   dS rq   r   rm   r   r   r   rs      s    zRGBAImageBuffer.has_animationNrt   r   r   r   r   rv      s
   
rv   c                   @   s,   e Zd Zedd Zedd Zdd ZdS )rG   c                 C   s   t dS )zq
        Willow internal name for the image format
        ImageFile implementations MUST override this.
        NNotImplementedErrorrm   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.
        Nrw   rm   r   r   r   	mime_type   s    zImageFile.mime_typec                 C   s
   || _ d S r   )r>   )r0   r>   r   r   r   rl      s    zImageFile.__init__N)r   r   r   propertyry   rz   rl   r   r   r   r   rG      s
   

rG   c                   @   s$   e Zd Zedd Zedd ZdS )JPEGImageFilec                 C   s   dS )NrI   r   rm   r   r   r   ry      s    zJPEGImageFile.format_namec                 C   s   dS )Nz
image/jpegr   rm   r   r   r   rz      s    zJPEGImageFile.mime_typeNr   r   r   r{   ry   rz   r   r   r   r   r|      s   
r|   c                   @   s$   e Zd Zedd Zedd ZdS )PNGImageFilec                 C   s   dS )NrJ   r   rm   r   r   r   ry     s    zPNGImageFile.format_namec                 C   s   dS )Nz	image/pngr   rm   r   r   r   rz     s    zPNGImageFile.mime_typeNr}   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 )NrK   r   rm   r   r   r   ry     s    zGIFImageFile.format_namec                 C   s   dS )Nz	image/gifr   rm   r   r   r   rz     s    zGIFImageFile.mime_typeNr}   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 )NrL   r   rm   r   r   r   ry     s    zBMPImageFile.format_namec                 C   s   dS )Nz	image/bmpr   rm   r   r   r   rz     s    zBMPImageFile.mime_typeNr}   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 )NrM   r   rm   r   r   r   ry     s    zTIFFImageFile.format_namec                 C   s   dS )Nz
image/tiffr   rm   r   r   r   rz   #  s    zTIFFImageFile.mime_typeNr}   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 )NrN   r   rm   r   r   r   ry   )  s    zWebPImageFile.format_namec                 C   s   dS )Nz
image/webpr   rm   r   r   r   rz   -  s    zWebPImageFile.mime_typeNr}   r   r   r   r   r   (  s   
r   c                       s&   e Zd ZdZdZd fdd	Z  ZS )SvgImageFiler7   zimage/svg+xmlNc                    s@   |d kr*| d t|| _| d n|| _t | d S )Nr   )rA   r   parsedomsuperrl   )r0   r>   r   r4   r   r   rl   6  s    
zSvgImageFile.__init__)N)r   r   r   ry   rz   rl   __classcell__r   r   r   r   r   2  s   r   c                   @   s$   e Zd Zedd Zedd ZdS )HeicImageFilec                 C   s   dS )NrO   r   rm   r   r   r   ry   B  s    zHeicImageFile.format_namec                 C   s   dS )Nz
image/heicr   rm   r   r   r   rz   F  s    zHeicImageFile.mime_typeNr}   r   r   r   r   r   A  s   
r   c                   @   s$   e Zd Zedd Zedd ZdS )AvifImageFilec                 C   s   dS )NrP   r   rm   r   r   r   ry   L  s    zAvifImageFile.format_namec                 C   s   dS )Nz
image/avifr   rm   r   r   r   rz   P  s    zAvifImageFile.mime_typeNr}   r   r   r   r   r   K  s   
r   c                   @   s   e Zd ZdZdZdS )IcoImageFilerQ   zimage/x-iconN)r   r   r   ry   rz   r   r   r   r   r   U  s   r   r7   ).r\   rB   ior   tempfiler   r   typingr   r9   Z
defusedxmlr   Zfiletype.typesr   Zimage_typesr	   IOErrorr
   rS   r   r   ri   ro   rv   rG   r|   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>   s^   	 %








          