
    @wg                         d dl Zd dlmZ d dlmZmZ d dlmZ  G d dej                  j                  j                        Zy)    N)Image)QRColorMaskSolidFillColorMask)SquareModuleDrawerc                   d     e Zd ZU dZdZdZeed<   eZ	 fdZ
d Z fdZd Zd	 Zdd
Zd Z xZS )StyledPilImageav  
    Styled PIL image builder, default format is PNG.

    This differs from the PilImage in that there is a module_drawer, a
    color_mask, and an optional image

    The module_drawer should extend the QRModuleDrawer class and implement the
    drawrect_context(self, box, active, context), and probably also the
    initialize function. This will draw an individual "module" or square on
    the QR code.

    The color_mask will extend the QRColorMask class and will at very least
    implement the get_fg_pixel(image, x, y) function, calculating a color to
    put on the image at the pixel location (x,y) (more advanced functionality
    can be gotten by instead overriding other functions defined in the
    QRColorMask class)

    The Image can be specified either by path or with a Pillow Image, and if it
    is there will be placed in the middle of the QR code. No effort is done to
    ensure that the QR code is still legible after the image has been placed
    there; Q or H level error correction levels are recommended to maintain
    data integrity A resampling filter can be specified (defaulting to
    PIL.Image.Resampling.LANCZOS) for resizing; see PIL.Image.resize() for possible
    options for this parameter.
    The image size can be controlled by `embeded_image_ratio` which is a ratio
    between 0 and 1 that's set in relation to the overall width of the QR code.
    PNGT
color_maskc                 h   |j                  dt                     | _        |j                  dd       }|j                  dd       | _        |j                  dd      | _        |j                  dt
        j                  j                        | _        | j                  s|rt        j                  |      | _        t        d | j                  j                  D              | _        | j                  j                  r+t        g | j                  j                  d d d	      | _        t        | <  |i | y )
Nr
   embeded_image_pathembeded_imageembeded_image_ratiog      ?embeded_image_resamplec              3       K   | ]  }d   yw)r   N ).0is     M/var/www/horilla/myenv/lib/python3.12/site-packages/qrcode/image/styledpil.py	<genexpr>z*StyledPilImage.__init__.<locals>.<genexpr>8   s      Gq Gs         )getr   r
   r   r   r   
ResamplingLANCZOSr   opentuple
back_colorpaint_colorhas_transparencysuper__init__)selfargskwargsr   	__class__s       r   r!   zStyledPilImage.__init__*   s     **\3E3GH#ZZ(<dC#ZZ>#)::.CT#J &,jj$e&6&6&>&>'
# !!&8!&,>!?D
 ! GDOO,F,F GG??++$%Kt'A'A"1'E%Ks%KLD$)&)    c                    | j                   j                  s(| j                  rd| j                  j                         v rdnd}| j                   j                  }t        j                  || j                  | j                  f|      S )NARGBARGB)r
   r   r   getbandsr   r   new
pixel_size)r"   r$   moder   s       r   	new_imagezStyledPilImage.new_image>   sp     00&&3$2D2D2M2M2O+O 
  	 __//
yyA:NNr&   c                 n    | j                   j                  | | j                         t        |           y N)r
   
initialize_imgr    init_new_image)r"   r%   s    r   r4   zStyledPilImage.init_new_imageL   s&    ""43 r&   c                     | j                   j                  | j                         | j                  r| j	                          y y r1   )r
   
apply_maskr3   r   draw_embeded_image)r"   s    r   processzStyledPilImage.processP   s2    ""499-##% r&   c                    | j                   sy | j                  j                  \  }}t        |      }t        || j                  z        }t        t        |dz        t        |dz        z
  | j
                  z        | j
                  z  }||f}||dz  z
  }| j                   }|j                  ||f| j                        }d|j                         v r| j                  j                  ||       y | j                  j                  ||       y )N   r(   )r   r3   sizeintr   box_sizeresizer   r+   alpha_compositepaste)r"   total_width_logo_width_ishlogo_offsetlogo_position
logo_widthregions           r   r7   z!StyledPilImage.draw_embeded_imageU   s    !!Q+&[4+C+CCD[1_%NQ,>(??4==PQmm 	 %k2 ;?2
##
J79T9TU&//##II%%fm<IIOOFM2r&   c                     ||j                  d| j                        }d|v r|d=  | j                  j                  |fd|i| y )Nkindformat)r   rI   r3   save)r"   streamrJ   r$   s       r   rK   zStyledPilImage.saveh   sF    >ZZ		2FVv		v7f77r&   c                 .    t        | j                  |      S r1   )getattrr3   )r"   names     r   __getattr__zStyledPilImage.__getattr__o   s    tyy$''r&   r1   )__name__
__module____qualname____doc__rI   needs_processingr   __annotations__r   default_drawer_classr!   r/   r4   r8   r7   rK   rP   __classcell__)r%   s   @r   r   r      sE    8 D-*(O!&
3&8(r&   r   )qrcode.image.baseqrcodePILr   qrcode.image.styles.colormasksr   r   !qrcode.image.styles.moduledrawersr   imagebaseBaseImageWithDrawerr   r   r&   r   <module>ra      s/      J @i(V\\&&:: i(r&   