o
    uX!0                     @   s   d dl Z 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	m
Z
mZmZ dgZddgZdd	iZd+ddZdd Zd add Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  ZG d!d" d"eZd,d#d$ZG d%d& d&eZd'd( Z d-d)d*Z!dS ).    N)UnsupportedOperation   )UnknownExtensionUnknownFormat)Image	ImageFileStringIOstring_typesPNGGIFJPEGz.jpgTc                 K   s   t | t |||S N)
save_imager   )imgformatautoconvertoptions r   A/var/www/jaguar/venv/lib/python3.10/site-packages/pilkit/utils.pyimg_to_fobj   s   r   c                 C   s   |  d t| S )Nr   )seekr   open)targetr   r   r   
open_image   s   

r   c                   C   s   t dk rt  da dS dS )zLoads the standard PIL file format drivers. Returns True if ``preinit()``
    was called (and there's a potential that more drivers were loaded) or False
    if there is no possibility that new drivers were loaded.

    r   TF)	_pil_initr   preinitr   r   r   r   _preinit_pil   s
   r   c                   C   s"   t   tdk rt  dadS dS )zLoads all PIL file format drivers. Returns True if ``init()`` was called
    (and there's a potential that more drivers were loaded) or False if there is
    no possibility that new drivers were loaded.

       TF)r   r   r   initr   r   r   r   	_init_pil*   s   r   c                 C   s   t j|  S r   )r   	EXTENSIONgetlower)	extensionr   r   r   _extension_to_format9   s   r$   c                 C   sR   | r'|   } | tv rt|  }|tjv r|S tj D ]\}}|| kr&|  S qd S r   )upperDEFAULT_EXTENSIONSr   r    items)r   extkvr   r   r   _format_to_extension=   s   
r+   c                 C   s:   zd| pd }t |d }W |S  ty   d }Y |S w )Nza%s r   )	mimetypes
guess_type
IndexError)r(   filenamemimetyper   r   r   extension_to_mimetypeM   s   r2   c                 C   s   t t| S r   )r2   format_to_extension)r   r   r   r   format_to_mimetypeV      r4   c                 C   s<   t | }|st rt | }|st rt | }|st| |S )zDReturns the format that corresponds to the provided extension.

    )r$   r   r   r   )r#   r   r   r   r   extension_to_formatZ   s   

r6   c                 C   sD   d}| rt | }|st rt | }|st rt | }|s t| |S )zCReturns the first extension that matches the provided format.

    N)r+   r   r   r   )r   r#   r   r   r   r3   h   s   

r3   c                 C   s   t j| d }zt|}W n ty   |}Y |S w | | kr'|}|S zt|}W n ty9   |}Y |S w | | krF|}|S |}|S )Nr   )ospathsplitextr3   r   r"   r6   r   )namer   original_extensionsuggested_extensionr#   original_formatr   r   r   suggest_extensionx   s*   r>   c                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )FileWrapperc                    s   t t| d| d S )N_wrapped)superr?   __setattr__)selfwrapped	__class__r   r   __init__   s   zFileWrapper.__init__c                 C   s    z| j  W S  ty   tw r   )r@   filenor   AttributeErrorrC   r   r   r   rH      s
   zFileWrapper.filenoc                 C      t | j|S r   )getattrr@   )rC   r:   r   r   r   __getattr__   r5   zFileWrapper.__getattr__c                 C   s   t | j||S r   )setattrr@   )rC   r:   valuer   r   r   rB      s   zFileWrapper.__setattr__c                 C   rK   r   )delattrr@   )rC   keyr   r   r   __delattr__   r5   zFileWrapper.__delattr__)	__name__
__module____qualname__rG   rH   rM   rB   rR   __classcell__r   r   rE   r   r?      s    r?   c           
         s:  pi |rt  \}| |zd W n	 ty$   Y nw  fdd}tfddtD r;}nt}z|| W nD ty   tdv rWt	d ndd j
d d d	j
d  j
d
  }|tjk rs tj}	|t_z	|| W |	t_n|	t_w Y nw zd W S  ty   Y S w )aR  
    Wraps PIL's ``Image.save()`` method. There are two main benefits of using
    this function over PIL's:

    1. It gracefully handles the infamous "Suspension not allowed here" errors.
    2. It prepares the image for saving using ``prepare_image()``, which will do
        some common-sense processing given the target format.

    r   c                    s@   t   j|  fi  W d    d S 1 sw   Y  d S r   )quietsave)fp)r   r   r   r   r   rX      s   "zsave_image.<locals>.savec                 3   s    | ]}t  |V  qd S r   )
isinstance).0t)outfiler   r   	<genexpr>   s    zsave_image.<locals>.<genexpr>exif         r   )prepare_imageupdater   rI   anyr	   r?   IOErrormaxlensizer   MAXBLOCK)
r   r]   r   r   r   save_kwargsrX   wrappernew_maxblockold_maxblockr   )r   r   r   r]   r   r      sJ   

	

r   c                   @   s    e Zd ZdZdd Zdd ZdS )rW   z
    A context manager for suppressing the stderr activity of PIL's C libraries.
    Based on http://stackoverflow.com/a/978264/155370

    c                 C   st   zt j | _W n
 ty   Y d S w zttjtj| _	W n
 t
y(   Y d S w t| j| _t| j	| j d S r   )sys
__stderr__rH   	stderr_fdrI   r7   r   devnullO_RDWRnull_fdOSErrordupolddup2rJ   r   r   r   	__enter__   s   zquiet.__enter__c                 O   sL   t | dd sd S t | dd sd S t| j| j t| j t| j d S )Nrt   rw   )rL   r7   rx   rw   rq   closert   )rC   argskwargsr   r   r   __exit__  s   zquiet.__exit__N)rS   rT   rU   __doc__ry   r}   r   r   r   r   rW      s    rW   c                 C   s2  d}i }|  }| jdkrB|tv rnj|tv r?|   |  d }t|dd }| djdtj	dd	} | 
d| d|d
< n=d}n:| jdkrk|tv r^z	| jd
 |d
< W n' ty]   Y nw |tv rh| d} nd}n| d} |dkr|| jdtj	d} |rddlm} | | d} |dkrd|d< | |fS )a  
    Prepares the image for saving to the provided format by doing some
    common-sense conversions. This includes things like preserving transparency
    and quantizing. This function is used automatically by ``save_image()``
    immediately before saving unless you specify ``autoconvert=False``. It is
    provided as a utility for those doing their own processing.

    :param img: The image to prepare for saving.
    :param format: The format that the image will be saved to.

    FRGBAc                 S   s   | dkrdS dS )N      r   r   )ar   r   r   <lambda>4  s    zprepare_image.<locals>.<lambda>RGBPr   )palettecolorstransparencyTr   )r   r   )
MakeOpaquer   optimize)r%   modeRGBA_TRANSPARENCY_FORMATSPALETTE_TRANSPARENCY_FORMATSloadsplitr   evalconvertADAPTIVEpasteinfoKeyError
processorsr   process)r   r   make_opaquerk   alphamaskr   r   r   r   rc     sF   

	

rc   c                 C   sR   ddl m} | j}||pg | } |p| jp|pd}|pi }t| ||fi |S )Nr   )ProcessorPipeliner   )r   r   r   r   r   )r   r   r   r   r   r   r=   r   r   r   process_imageb  s   r   )T)NT)NNTN)"r7   r-   ro   ior   
exceptionsr   r   libr   r   r   r	   r   r   r&   r   r   r   r   r   r$   r+   r2   r4   r6   r3   r>   objectr?   r   rW   rc   r   r   r   r   r   <module>   s6    
	
G W