1- import qrcode .image .base
1+ from __future__ import annotations
2+
3+ import warnings
4+ from typing import overload
5+
6+ import deprecation
27from PIL import Image
8+
9+ import qrcode .image .base
310from qrcode .image .styles .colormasks import QRColorMask , SolidFillColorMask
411from qrcode .image .styles .moduledrawers import SquareModuleDrawer
512
@@ -29,7 +36,7 @@ class StyledPilImage(qrcode.image.base.BaseImageWithDrawer):
2936 data integrity A resampling filter can be specified (defaulting to
3037 PIL.Image.Resampling.LANCZOS) for resizing; see PIL.Image.resize() for possible
3138 options for this parameter.
32- The image size can be controlled by `embeded_image_ratio ` which is a ratio
39+ The image size can be controlled by `embedded_image_ratio ` which is a ratio
3340 between 0 and 1 that's set in relation to the overall width of the QR code.
3441 """
3542
@@ -41,14 +48,32 @@ class StyledPilImage(qrcode.image.base.BaseImageWithDrawer):
4148
4249 def __init__ (self , * args , ** kwargs ):
4350 self .color_mask = kwargs .get ("color_mask" , SolidFillColorMask ())
44- embeded_image_path = kwargs .get ("embeded_image_path" , None )
45- self .embeded_image = kwargs .get ("embeded_image" , None )
46- self .embeded_image_ratio = kwargs .get ("embeded_image_ratio" , 0.25 )
47- self .embeded_image_resample = kwargs .get (
48- "embeded_image_resample" , Image .Resampling .LANCZOS
51+
52+ if kwargs .get ("embeded_image_path" ) or kwargs .get ("embeded_image" ):
53+ warnings .warn (
54+ "The 'embeded_*' parameters are deprecated. Use 'embedded_image_path' "
55+ "or 'embedded_image' instead. The 'embeded_*' parameters will be "
56+ "removed in v9.0." ,
57+ category = DeprecationWarning ,
58+ stacklevel = 2 ,
59+ )
60+
61+ # allow embeded_ parameters with typos for backwards compatibility
62+ embedded_image_path = kwargs .get (
63+ "embedded_image_path" , kwargs .get ("embeded_image_path" , None )
64+ )
65+ self .embedded_image = kwargs .get (
66+ "embedded_image" , kwargs .get ("embeded_image" , None )
67+ )
68+ self .embedded_image_ratio = kwargs .get (
69+ "embedded_image_ratio" , kwargs .get ("embeded_image_ratio" , 0.25 )
70+ )
71+ self .embedded_image_resample = kwargs .get (
72+ "embedded_image_resample" ,
73+ kwargs .get ("embeded_image_resample" , Image .Resampling .LANCZOS ),
4974 )
50- if not self .embeded_image and embeded_image_path :
51- self .embeded_image = Image .open (embeded_image_path )
75+ if not self .embedded_image and embedded_image_path :
76+ self .embedded_image = Image .open (embedded_image_path )
5277
5378 # the paint_color is the color the module drawer will use to draw upon
5479 # a canvas During the color mask process, pixels that are paint_color
@@ -59,12 +84,18 @@ def __init__(self, *args, **kwargs):
5984
6085 super ().__init__ (* args , ** kwargs )
6186
87+ @overload
88+ def drawrect (self , row , col ):
89+ """
90+ Not used.
91+ """
92+
6293 def new_image (self , ** kwargs ):
6394 mode = (
6495 "RGBA"
6596 if (
6697 self .color_mask .has_transparency
67- or (self .embeded_image and "A" in self .embeded_image .getbands ())
98+ or (self .embedded_image and "A" in self .embedded_image .getbands ())
6899 )
69100 else "RGB"
70101 )
@@ -79,23 +110,32 @@ def init_new_image(self):
79110
80111 def process (self ):
81112 self .color_mask .apply_mask (self ._img )
82- if self .embeded_image :
83- self .draw_embeded_image ()
84-
113+ if self .embedded_image :
114+ self .draw_embedded_image ()
115+
116+ @deprecation .deprecated (
117+ deprecated_in = "9.0" ,
118+ removed_in = "8.3" ,
119+ current_version = "8.2" ,
120+ details = "Use draw_embedded_image() instead" ,
121+ )
85122 def draw_embeded_image (self ):
86- if not self .embeded_image :
123+ return self .draw_embedded_image ()
124+
125+ def draw_embedded_image (self ):
126+ if not self .embedded_image :
87127 return
88128 total_width , _ = self ._img .size
89129 total_width = int (total_width )
90- logo_width_ish = int (total_width * self .embeded_image_ratio )
130+ logo_width_ish = int (total_width * self .embedded_image_ratio )
91131 logo_offset = (
92132 int ((int (total_width / 2 ) - int (logo_width_ish / 2 )) / self .box_size )
93133 * self .box_size
94134 ) # round the offset to the nearest module
95135 logo_position = (logo_offset , logo_offset )
96136 logo_width = total_width - logo_offset * 2
97- region = self .embeded_image
98- region = region .resize ((logo_width , logo_width ), self .embeded_image_resample )
137+ region = self .embedded_image
138+ region = region .resize ((logo_width , logo_width ), self .embedded_image_resample )
99139 if "A" in region .getbands ():
100140 self ._img .alpha_composite (region , logo_position )
101141 else :
@@ -104,8 +144,7 @@ def draw_embeded_image(self):
104144 def save (self , stream , format = None , ** kwargs ):
105145 if format is None :
106146 format = kwargs .get ("kind" , self .kind )
107- if "kind" in kwargs :
108- del kwargs ["kind" ]
147+ kwargs .pop ("kind" , None )
109148 self ._img .save (stream , format = format , ** kwargs )
110149
111150 def __getattr__ (self , name ):
0 commit comments