o
    h#                     @   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   L/var/www/html/ndineBlogger/venv/lib/python3.10/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ded fddZdefddZdS )Imagec                 C   s   d S Nr   )clsr   r   r   check      zImage.checkc                 C   s
   d| _ | S NT)_willow_operationfuncr   r   r   	operation!   s   zImage.operationNc                        fdd}|S )Nc                    s    f| _ | S r   )_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                    r   )Nc                    sL   t | dsg | _ttr| j fddD  | S | j f | S )N_willow_converter_fromc                    s   g | ]}| fqS r   r   ).0sc)r    r   r   
<listcomp>5   s    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                    sV   zt t|\} }W n ty    tjjd|w  fdd}|S )Nz object has no attribute c                     s0   } D ]\}}||}q|g| R i |S r   r   )argskwargsr   	converter_conversion_pathr   selfr   r   r"   F   s   
z"Image.__getattr__.<locals>.wrapper)r
   find_operationtypeLookupErrorAttributeError	__class__r   )r6   attrr3   r"   r   r4   r   __getattr__=   s   zImage.__getattr__c                 C   sX   t |}|d u r| |rd}t|}|s(|r$td| dtd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_format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   rE   patternliner   r   r   rB   f   s   




zImage.maybe_xmlTreturn	ImageFilec                 C   s0   |dvrt d| d| }t| |||dS )N)
jpegpnggifbmptiffwebpr>   heicavificozUnknown image format: save_as_)apply_optimizers)
ValueErrorgetattr)r6   rF   outputr[   operation_namer   r   r   saves   s   z
Image.saverF   c           	   	   C   s  t |}|s	dS d}zt|ttfr8tdd}d}|d t|| |j}W d   n1 s2w   Y  n@t	|drA|j}n7t|t
rI|}n/t|trntdd}|| |j}d}W d   n1 shw   Y  n
tdt| d|D ]}|| qzt	|d	r|d t|d
}t|| W d   n1 sw   Y  t	|dr|  W |rt| dS dS |rt| w w )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 filerI   rbtruncate)r
   get_optimizers_for_formatr)   r   r   r   rI   r   rb   r(   strbyteswrite	TypeErrorr8   processrH   rd   osunlink)	r6   
image_filerF   
optimizersnamed_file_created
named_file	file_path	optimizerrE   r   r   r   optimize   sT   
	








zImage.optimizer   )T)r   r   r   classmethodr   staticmethodr   r#   r/   r=   rH   rB   r   r`   rf   rs   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)r6   rw   rx   r   r   r   __init__   s   
zImageBuffer.__init__c                 C   s   | j S r   )rw   r6   r   r   r   get_size   s   zImageBuffer.get_sizeN)r   r   r   ry   r   r   r{   r   r   r   r   rv      s    rv   c                   @   ,   e Zd ZdZejdd Zejdd ZdS )RGBImageBufferRGBc                 C      dS NFr   rz   r   r   r   	has_alpha   r   zRGBImageBuffer.has_alphac                 C   r   r   r   rz   r   r   r   has_animation   r   zRGBImageBuffer.has_animationNr   r   r   moder   r   r   r   r   r   r   r   r}          
r}   c                   @   r|   )RGBAImageBufferRGBAc                 C   r   r   r   rz   r   r   r   r      r   zRGBAImageBuffer.has_alphac                 C   r   r   r   rz   r   r   r   r      r   zRGBAImageBuffer.has_animationNr   r   r   r   r   r      r   r   c                   @   s,   e Zd Zedd Zedd Zdd ZdS )rP   c                 C      t )zq
        Willow internal name for the image format
        ImageFile implementations MUST override this.
        NotImplementedErrorrz   r   r   r   format_name      zImageFile.format_namec                 C   r   )zo
        Returns the MIME type of the image file
        ImageFile implementations MUST override this.
        r   rz   r   r   r   	mime_type   r   zImageFile.mime_typec                 C   s
   || _ d S r   )rE   )r6   rE   r   r   r   ry      s   
zImageFile.__init__N)r   r   r   propertyr   r   ry   r   r   r   r   rP      s    

rP   c                   @   $   e Zd Zedd Zedd ZdS )JPEGImageFilec                 C   r   )NrQ   r   rz   r   r   r   r      r   zJPEGImageFile.format_namec                 C   r   )Nz
image/jpegr   rz   r   r   r   r      r   zJPEGImageFile.mime_typeNr   r   r   r   r   r   r   r   r   r   r      
    
r   c                   @   r   )PNGImageFilec                 C   r   )NrR   r   rz   r   r   r   r     r   zPNGImageFile.format_namec                 C   r   )Nz	image/pngr   rz   r   r   r   r     r   zPNGImageFile.mime_typeNr   r   r   r   r   r     r   r   c                   @   r   )GIFImageFilec                 C   r   )NrS   r   rz   r   r   r   r     r   zGIFImageFile.format_namec                 C   r   )Nz	image/gifr   rz   r   r   r   r     r   zGIFImageFile.mime_typeNr   r   r   r   r   r     r   r   c                   @   r   )BMPImageFilec                 C   r   )NrT   r   rz   r   r   r   r     r   zBMPImageFile.format_namec                 C   r   )Nz	image/bmpr   rz   r   r   r   r     r   zBMPImageFile.mime_typeNr   r   r   r   r   r     r   r   c                   @   r   )TIFFImageFilec                 C   r   )NrU   r   rz   r   r   r   r   "  r   zTIFFImageFile.format_namec                 C   r   )Nz
image/tiffr   rz   r   r   r   r   &  r   zTIFFImageFile.mime_typeNr   r   r   r   r   r   !  r   r   c                   @   r   )WebPImageFilec                 C   r   )NrV   r   rz   r   r   r   r   ,  r   zWebPImageFile.format_namec                 C   r   )Nz
image/webpr   rz   r   r   r   r   0  r   zWebPImageFile.mime_typeNr   r   r   r   r   r   +  r   r   c                       s&   e Zd ZdZdZd fdd	Z  ZS )SvgImageFiler>   zimage/svg+xmlNc                    s@   |d u r| d t|| _| d n|| _t | d S )Nr   )rI   r   parsedomsuperry   )r6   rE   r   r;   r   r   ry   9  s   
zSvgImageFile.__init__r   )r   r   r   r   r   ry   __classcell__r   r   r   r   r   5  s    r   c                   @   r   )HeicImageFilec                 C   r   )NrW   r   rz   r   r   r   r   E  r   zHeicImageFile.format_namec                 C   r   )Nz
image/heicr   rz   r   r   r   r   I  r   zHeicImageFile.mime_typeNr   r   r   r   r   r   D  r   r   c                   @   r   )AvifImageFilec                 C   r   )NrX   r   rz   r   r   r   r   O  r   zAvifImageFile.format_namec                 C   r   )Nz
image/avifr   rz   r   r   r   r   S  r   zAvifImageFile.mime_typeNr   r   r   r   r   r   N  r   r   c                   @   s   e Zd ZdZdZdS )IcoImageFilerY   zimage/x-iconN)r   r   r   r   r   r   r   r   r   r   X  s    r   r>   )0rk   rJ   ior   shutilr   tempfiler   r   typingr   r@   
defusedxmlr   filetype.typesr   image_typesr
   IOErrorr   r\   r   r   rv   r}   r   rP   r   r   r   r   r   r   r   r   r   r   Jpeg	extensionPngGifBmpTiffWebpHeicAvifIcorC   r   r   r   r   <module>   sN    	 '

















