Closed artemox closed 2 years ago
This code fails either:
qrimg = qrgen.make_image(
image_factory=StyledPilImage,
module_drawer=RoundedModuleDrawer(),
color_mask=RadialGradiantColorMask(back_color=(1, 1, 1, 0), center_color=(126, 82, 161), edge_color=(50, 82, 50)),
)
Traceback:
File "\lib\site-packages\qrcode\main.py", line 309, in make_image
im.process()
File "\lib\site-packages\qrcode\image\styledpil.py", line 78, in process
self.color_mask.apply_mask(self._img)
File "\lib\site-packages\qrcode\image\styles\colormasks.py", line 40, in apply_mask
image.putpixel((x,y), self.interp_color(self.get_bg_pixel(image, x,y), self.get_fg_pixel(image, x,y), norm))
File "\lib\site-packages\qrcode\image\styles\colormasks.py", line 58, in interp_color
return tuple(self.interp_num(col1[i], col2[i], norm) for i in range(len(col1)))
File "\lib\site-packages\qrcode\image\styles\colormasks.py", line 58, in <genexpr>
return tuple(self.interp_num(col1[i], col2[i], norm) for i in range(len(col1)))
IndexError: tuple index out of range
Here's a working example with your radial color gradient:
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer
from qrcode.image.styles.colormasks import RadialGradiantColorMask
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=20,
border=4,
)
qr.add_data('https://example.com/?utm_source=foo&utm_medium=bar&utm_campaign=baz')
qr.make(fit=True)
img = qr.make_image(
image_factory=StyledPilImage,
color_mask=RadialGradiantColorMask(
center_color=(126, 82, 161, 255), edge_color=(50, 50, 50, 255), back_color=(255, 255, 255, 0)),
)
img.save('qr-code-radial-with-transparent-background.png')
And here's a simpler example for people who just want a regular QR code with a transparent background:
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.colormasks import SolidFillColorMask
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=20,
border=4,
)
qr.add_data('https://example.com/?utm_source=foo&utm_medium=bar&utm_campaign=baz')
qr.make(fit=True)
img = qr.make_image(
image_factory=StyledPilImage,
color_mask=SolidFillColorMask(front_color=(0,0,0,255), back_color=(255,255,255,0)),
)
img.save('qr-code-with-transparent-background.png')
Thanks!
Here's a working example with your radial color gradient:
This code is working correctly:
This code fails:
Traceback: