Skip to content

Commit c7b47c6

Browse files
committed
feat: improve performance of SolidFillColorMask apply_mask function
1 parent cd3cd7b commit c7b47c6

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

qrcode/image/styles/colormasks.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ def apply_mask(self, image):
9797
# black and white, so if these are also our mask colors we don't
9898
# need to do anything. This is much faster than actually applying a
9999
# mask.
100-
pass
101-
else:
102-
# TODO there's probably a way to use PIL.ImageMath instead of doing
103-
# the individual pixel comparisons that the base class uses, which
104-
# would be a lot faster. (In fact doing this would probably remove
105-
# the need for the B&W optimization above.)
106-
QRColorMask.apply_mask(self, image)
100+
return
101+
102+
image.paste(
103+
Image.composite(
104+
Image.new("RGB", image.size, self.front_color),
105+
Image.new("RGB", image.size, self.back_color),
106+
image.convert("L").point(lambda p: 255 if p < 128 else 0),
107+
)
108+
)
107109

108110
def get_fg_pixel(self, image, x, y):
109111
return self.front_color

0 commit comments

Comments
 (0)