U
    ;ßôg}  ã                   @   sV   d dl mZ d dlZd dlmZ d dlmZmZ d dl	m
Z
 G dd„ dejjjƒZdS )é    )Úabsolute_importN)ÚImage)ÚQRColorMaskÚSolidFillColorMask)ÚSquareModuleDrawerc                       sl   e Zd ZU dZdZdZeed< eZ	‡ fdd„Z
dd„ Z‡ fd	d
„Zdd„ Zdd„ Zddd„Zdd„ Z‡  Z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
    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.
    ZPNGTÚ
color_maskc                    sž   |  dtƒ ¡| _|  dd ¡}|  dd ¡| _|  dtjj¡| _| jsR|rRt |¡| _t	dd„ | jj
D ƒƒ| _| jjrŒt	| jj
d d… d	•ƒ| _tƒ j||Ž d S )
Nr   Úembeded_image_pathÚembeded_imageÚembeded_image_resamplec                 s   s   | ]
}d V  qdS )r   N© )Ú.0Úir   r   ú:/tmp/pip-unpacked-wheel-ij2mbwps/qrcode/image/styledpil.pyÚ	<genexpr>8   s     z*StyledPilImage.__init__.<locals>.<genexpr>é   éÿ   )r   )Úgetr   r   r
   r   Z
ResamplingZLANCZOSr   ÚopenÚtupleÚ
back_colorZpaint_colorÚhas_transparencyÚsuperÚ__init__)ÚselfÚargsÚkwargsr	   ©Ú	__class__r   r   r   +   s     ÿ
zStyledPilImage.__init__c                 K   sB   | j js| jr d| j ¡ kr dnd}| j j}t || j| jf|¡S )NÚAZRGBAZRGB)r   r   r
   Úgetbandsr   r   ÚnewZ
pixel_size)r   r   Úmoder   r   r   r   Ú	new_image>   s    þýýú	zStyledPilImage.new_imagec                    s   | j  | | j¡ tƒ  ¡  d S ©N)r   Z
initializeÚ_imgr   Úinit_new_image©r   r   r   r   r&   L   s    zStyledPilImage.init_new_imagec                 C   s    | j  | j¡ | jr|  ¡  d S r$   )r   Z
apply_maskr%   r
   Údraw_embeded_imager'   r   r   r   ÚprocessP   s    zStyledPilImage.processc                 C   s¬   | j s
d S | jj\}}t|ƒ}t|d ƒ}tt|d ƒt|d ƒ | j ƒ| j }||f}||d  }| j }| ||f| j¡}d| ¡ krš| j ||¡ n| j 	||¡ d S )Né   é   r   )
r
   r%   ÚsizeÚintZbox_sizeÚresizer   r    Zalpha_compositeZpaste)r   Ztotal_widthÚ_Zlogo_width_ishZlogo_offsetZlogo_positionZ
logo_widthZregionr   r   r   r(   U   s      ÿÿz!StyledPilImage.draw_embeded_imageNc                 K   s@   |d kr|  d| j¡}d|kr$|d= | jj|fd|i|—Ž d S )NÚkindÚformat)r   r0   r%   Úsave)r   Ústreamr1   r   r   r   r   r2   h   s
    zStyledPilImage.savec                 C   s   t | j|ƒS r$   )Úgetattrr%   )r   Únamer   r   r   Ú__getattr__o   s    zStyledPilImage.__getattr__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   Zneeds_processingr   Ú__annotations__r   Zdefault_drawer_classr   r#   r&   r)   r(   r2   r6   Ú__classcell__r   r   r   r   r   
   s   

r   )Ú
__future__r   Zqrcode.image.baseZqrcodeZqrcode.compat.pilr   Zqrcode.image.styles.colormasksr   r   Z!qrcode.image.styles.moduledrawersr   ÚimageÚbaseZBaseImageWithDrawerr   r   r   r   r   Ú<module>   s
   