o
    |XWa{                     @   s~   d dl mZ z
d dlmZmZ W n ey!   d dlZd dlZY nw d dlZd dlm	Z	 d dl
mZ G dd dejjjZdS )    )absolute_import)Image	ImageDrawN)SolidFillColorMask)SquareModuleDrawerc                   @   sV   e Zd ZdZdZdZdZdd Zdd Zdd	 Z	d
d Z
dd ZdddZdd ZdS )StyledPilImagea  
    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 intitialize
    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.LANCZOS) for resizing; see PIL.Image.resize() for
    possible options for this parameter.
    PNGTc                 K   s(  | dt | _| dt | _| dt | _| dd }| dd | _| dtj| _	| js8|r8t
|| _| jjsF| jrHd| j v rHdnd	}|| _| jj| _t|| j| jf| j}td
d | jjD | _| jjr}tg | jjd d d| _| j| | | j| | | j| | |S )N
color_maskmodule_drawer
eye_drawerembeded_image_pathembeded_imageembeded_image_resampleARGBARGBc                 s   s    | ]}d V  qdS )r   N ).0ir   r   L/var/www/chikooza/env/lib/python3.10/site-packages/qrcode/image/styledpil.py	<genexpr>=   s    z+StyledPilImage.new_image.<locals>.<genexpr>      )getr   r	   r   r
   r   r   r   LANCZOSr   openhas_transparencygetbandsmode
back_colornew
pixel_sizetuplepaint_color
initialize)selfkwargsr   r   imgr   r   r   	new_image(   s&   
$
zStyledPilImage.new_imagec                 C   s@   |  ||}| ||r| j||| d S | j||| d S N)	pixel_boxis_eyer   drawrect_contextr
   )r%   rowcol	is_activecontextboxr   r   r   r,   F   s   zStyledPilImage.drawrect_contextc                 C   s$   | j | j | jr|   d S d S r)   )r	   
apply_mask_imgr   draw_embeded_image)r%   r   r   r   processM   s   zStyledPilImage.processc                 C   s   | j j\}}t|}t|d }tt|d t|d  | j | j }||f}||d  }| j}|||f| j}d| v rI| j || d S | j 	|| d S )N      r   )
r3   sizeintbox_sizer   resizer   r   alpha_compositepaste)r%   total_width_logo_width_ishlogo_offsetlogo_position
logo_widthregionr   r   r   r4   R   s   (z!StyledPilImage.draw_embeded_imagec                 C   s<   |dk r|dk p|dk o| j | dk p| j | dk o|dk S )N      )width)r%   r-   r.   r   r   r   r+   a   s
   zStyledPilImage.is_eyeNc                 K   s@   |d u r| d| j}d|v r|d= | jj|fd|i| d S )Nkindformat)r   rH   r3   save)r%   streamrI   r&   r   r   r   rJ   h   s
   zStyledPilImage.savec                 C   s   t | j|S r)   )getattrr3   )r%   namer   r   r   __getattr__o   s   zStyledPilImage.__getattr__r)   )__name__
__module____qualname____doc__rH   needs_contextneeds_processingr(   r,   r5   r4   r+   rJ   rN   r   r   r   r   r      s    
r   )
__future__r   PILr   r   ImportErrorqrcode.image.baseqrcodeqrcode.image.styles.colormasksr   !qrcode.image.styles.moduledrawersr   imagebase	BaseImager   r   r   r   r   <module>   s   