
    h                     x    d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ  G d d	      Zdd
ZddZy)    N)Path)File)cached_property)	renderSVG)Color)svg2rlgc                   |    e Zd ZdZddZed        Zed        Zed        Z	d Z
d Zd Zdd
Zd Zd Zd ZddZy	)Imagez_
    Attempting to be compatible with PIL's Image, but suitable for reportlab's SVGCanvas.
    c                    t        |t        t        f      r@t        |      dk(  r2t        |d   t        t
        f      rt        |d   t        t
        f      sJ d       t        j                  |d      | _        d | _	        y )N   r      z4Expected `size` as tuple with two floats or integersT)sizeuseClip)

isinstancelisttuplelenintfloatr   	SVGCanvascanvasmode)selfr   s     I/home/dcms/DCMS/lib/python3.12/site-packages/easy_thumbnails/VIL/Image.py__init__zImage.__init__   sp    $u.3t9>47S%L1ja3PU,6W	CB	C X  ))tTB	    c                 2    | j                   | j                  fS N)widthheightr   s    r   r   z
Image.size   s    zz4;;&&r   c                     	 t        | j                  j                  j                  d            S # t        $ r | j                         d   cY S w xY w)Nr   r   r   r   svggetAttribute
ValueErrorgetbboxr!   s    r   r   zImage.width   sE    	%55g>?? 	%<<>!$$	%   -0 AAc                     	 t        | j                  j                  j                  d            S # t        $ r | j                         d   cY S w xY w)Nr       r#   r!   s    r   r    zImage.height#   sE    	%55h?@@ 	%<<>!$$	%r(   c                     t        d | j                  j                  j                  d      j	                         D              S )z
        Calculates the bounding box of the non-zero regions in the image.

        :returns: The bounding box is returned as a 4-tuple defining the
           left, upper, right, and lower pixel coordinate.
        c              3   2   K   | ]  }t        |        y wr   )r   ).0bs     r   	<genexpr>z Image.getbbox.<locals>.<genexpr>1   s     W!U1XWs   viewBox)r   r   r$   r%   splitr!   s    r   r'   zImage.getbbox*   s1     Wt{{'C'CI'N'T'T'VWWWr   c                 N   t               }| j                  j                  j                  d      |j                  _        |j                  j                  j	                  d dj
                  |        |j                  j                  j	                  d dj
                  |        |S )z
        :param size: The requested size in pixels, as a 2-tuple: (width, height).
        :returns: The resized :py:class:`easy_thumbnails.VIL.Image.Image` object.
        Tr   {0}r    {1})r
   r   r$   	cloneNodesetAttributeformat)r   r   kwargscopys       r   resizezImage.resize3   st    
 w++//33D9$$WlellD.AB$$X|u||T/BCr   c                     | S z
        Does nothing, just for compatibility with PIL.
        :returns: An :py:class:`easy_thumbnails.VIL.Image.Image` object.
         r   argss     r   convertzImage.convert>   	    
 r   Nc                 $   t        | j                        }| j                  j                  j	                  d      |j                  _        |rBt        | j                               }|d   |d   z
  |d   |d   z
  z  }|d   |d   z
  |d   |d   z
  z  }||kD  r$||d   z  }|dxx   |d   |z
  dz  z  cc<   ||d<   n#|d   |z  }|dxx   |d   |z
  dz  z  cc<   ||d<   |d   |d   z
  |d   |d   z
  f}|j                  j                  j                  d dj                  |        |j                  j                  j                  d	 d
j                  |        |j                  j                  j                  d dj                  |        |S )aD  
        Returns a rectangular region from this image. The box is a
        4-tuple defining the left, upper, right, and lower pixel
        coordinate.

        :param box: The crop rectangle, as a (left, upper, right, lower)-tuple.
        :returns: The cropped :py:class:`easy_thumbnails.VIL.Image.Image` object.
        r   Tr   r   r*   r   r0   z{0} {1} {2} {3}r   r3   r    r4   )	r
   r   r   r$   r5   r   r'   r6   r7   )	r   boxr9   bboxcurrent_aspect_ratiowanted_aspect_ratio	new_width
new_heightr   s	            r   cropz
Image.cropE   s    $))$++//33D9'D$(Gd1g$5$q'DG:K#L #&q6CF?s1vA"G#&99/$q'9	QDGi/144#Q!!W'::
QDGj0A55$Qq6CF?CFSVO3DKKOO((4L4E4L4Ld4STKKOO((,%,,2EFKKOO((<5<<3FGr   c                     | S r<   r=   r>   s     r   filterzImage.filterb   rA   r   c                     | S r   r=   r!   s    r   	__enter__zImage.__enter__i   s    r   c                      y r   r=   )r   typevalue	tracebacks       r   __exit__zImage.__exit__l   s    r   c                    d}d}t        |t        t        f      r|}d}nt        |t              rt        |      }d}t        |      j                  j                         }|dk7  r|dk7  rt        d      |rt        j                  |d      }| j                  j                  j                  |       |r|j                          yy)	a}  
        Saves this image under the given filename.  If no format is
        specified, the format to use is determined from the filename
        extension, if possible.

        You can use a file object instead of a filename. In this case,
        you must always specify the format. The file object must
        implement the ``seek``, ``tell``, and ``write``
        methods, and be opened in binary mode.

        :param fp: A filename (string), pathlib.Path object or file object.
        :param format: Must be None or 'SVG'.
        :param params: Unused extra parameters.
        :returns: None
        :exception ValueError: If the output format could not be determined
           from the file name.  Use the format option to solve this.
        :exception OSError: If the file could not be written.  The file
           may have been created, and may contain partial data.
         FTSVGz.svgzAImage format is expected to be 'SVG' and file suffix to be '.svg'wN)r   bytesstrr   suffixlowerr&   builtinsopenr   r$   writexmlflush)r   fpr7   paramsfilenameopen_fprZ   s          r   savez
Image.saveo   s    * b5#,'HGD!2wHGh&&,,.U?v/`aax-B  $HHJ r   )),  re   r   )__name__
__module____qualname____doc__r   propertyr   r   r   r    r'   r:   r@   rJ   rL   rN   rS   rd   r=   r   r   r
   r
      sq     ' ' % % % %X	:&r   r
   c                 b    t        |      }|r!|j                  j                  t        |        |S r   )r
   r   setFillColorr   )r   r   colorims       r   newro      s)    	tB
		ue}-Ir   c                    |dk7  rt        dj                  |            t        | t              rt	        | j                               }n$t        | t        t        f      r| }nt        d      t        |      }|yt        |j                  |j                  f      }t        j                  ||j                         |S )a[  
    Opens and identifies the given SVG image file.

    :param fp: A filename (string), pathlib.Path object or a file object.
       The file object must implement :py:meth:`~file.read`,
       :py:meth:`~file.seek`, and :py:meth:`~file.tell` methods,
       and be opened in binary mode.
    :param mode: The mode.  If given, this argument must be "r".
    :returns: An :py:class:`easy_thumbnails.VIL.Image.Image` object.
    :exception FileNotFoundError: If the file cannot be found.
    :exception ValueError: If the ``mode`` is not "r", or if a ``StringIO``
       instance is used for ``fp``.
    rzbad mode {}zCan not open file.NrC   )r&   r7   r   r   rY   resolver   RuntimeErrorr   r
   r   r    r   drawr   )r`   r   rb   drawingrn   s        r   loadrv      s     s{--d344"drzz|$	Bs	$/00hG	W]]GNN3	4BNN7BII&Ir   r   )rq   )r\   pathlibr   django.core.filesr   django.utils.functionalr   reportlab.graphicsr   reportlab.lib.colorsr   svglib.svglibr   r
   ro   rv   r=   r   r   <module>r}      s1      " 3 ( & !H HVr   