From c7b47c634211c59e9b996d6ae5fa30506ea8040e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Sat, 19 Apr 2025 03:28:40 +0200 Subject: [PATCH] feat: improve performance of SolidFillColorMask apply_mask function --- qrcode/image/styles/colormasks.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/qrcode/image/styles/colormasks.py b/qrcode/image/styles/colormasks.py index 43186b27..b74905f7 100644 --- a/qrcode/image/styles/colormasks.py +++ b/qrcode/image/styles/colormasks.py @@ -97,13 +97,15 @@ def apply_mask(self, image): # black and white, so if these are also our mask colors we don't # need to do anything. This is much faster than actually applying a # mask. - pass - else: - # TODO there's probably a way to use PIL.ImageMath instead of doing - # the individual pixel comparisons that the base class uses, which - # would be a lot faster. (In fact doing this would probably remove - # the need for the B&W optimization above.) - QRColorMask.apply_mask(self, image) + return + + image.paste( + Image.composite( + Image.new("RGB", image.size, self.front_color), + Image.new("RGB", image.size, self.back_color), + image.convert("L").point(lambda p: 255 if p < 128 else 0), + ) + ) def get_fg_pixel(self, image, x, y): return self.front_color