Skip to content

Commit a5c9ee0

Browse files
authored
pil: Add GappedCircleModuleDrawer class
1 parent d44a409 commit a5c9ee0

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

qrcode/image/styles/moduledrawers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# For backwards compatibility, importing the PIL drawers here.
22
try:
33
from .pil import CircleModuleDrawer # noqa: F401
4+
from .pil import GappedCircleModuleDrawer # noqa: F401
45
from .pil import GappedSquareModuleDrawer # noqa: F401
56
from .pil import HorizontalBarsDrawer # noqa: F401
67
from .pil import RoundedModuleDrawer # noqa: F401

qrcode/image/styles/moduledrawers/pil.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,39 @@ def drawrect(self, box, is_active: bool):
9494
self.img._img.paste(self.circle, (box[0][0], box[0][1]))
9595

9696

97+
class GappedCircleModuleDrawer(StyledPilQRModuleDrawer):
98+
"""
99+
Draws the modules as circles that are not contiguous.
100+
101+
The size_ratio determines how wide the circles are relative to the width of
102+
the space they are printed in
103+
"""
104+
105+
circle = None
106+
107+
def __init__(self, size_ratio=0.9):
108+
self.size_ratio = size_ratio
109+
110+
def initialize(self, *args, **kwargs):
111+
super().initialize(*args, **kwargs)
112+
box_size = self.img.box_size
113+
fake_size = box_size * ANTIALIASING_FACTOR
114+
self.circle = Image.new(
115+
self.img.mode,
116+
(fake_size, fake_size),
117+
self.img.color_mask.back_color,
118+
)
119+
ImageDraw.Draw(self.circle).ellipse(
120+
(0, 0, fake_size, fake_size), fill=self.img.paint_color
121+
)
122+
smaller_size = int(self.size_ratio * box_size)
123+
self.circle = self.circle.resize((smaller_size, smaller_size), Image.Resampling.LANCZOS)
124+
125+
def drawrect(self, box, is_active: bool):
126+
if is_active:
127+
self.img._img.paste(self.circle, (box[0][0], box[0][1]))
128+
129+
97130
class RoundedModuleDrawer(StyledPilQRModuleDrawer):
98131
"""
99132
Draws the modules with all 90 degree corners replaced with rounded edges.

0 commit comments

Comments
 (0)