lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.34k stars 664 forks source link

ValueError: unknown color specifier: 'transparent' #264

Closed artemox closed 2 years ago

artemox commented 2 years ago

This code is working correctly:

    img = qrgen.make_image(
        fill_color=(126, 82, 161),
        back_color="transparent",
    )

This code fails:

    img = qrgen.make_image(
        image_factory=StyledPilImage,
        module_drawer=RoundedModuleDrawer(),
        color_mask=RadialGradiantColorMask(back_color="transparent", center_color=(126, 82, 161), edge_color=(50, 50, 50))
    )

Traceback:

  File "\lib\site-packages\qrcode\main.py", line 299, in make_image
    im = image_factory(
  File "\lib\site-packages\qrcode\image\base.py", line 17, in __init__
    self._img = self.new_image(**kwargs)
  File "\lib\site-packages\qrcode\image\styledpil.py", line 57, in new_image
    img = Image.new(mode, (self.pixel_size, self.pixel_size), self.back_color)
  File "\lib\site-packages\PIL\Image.py", line 2696, in new
    color = ImageColor.getcolor(color, mode)
  File "\lib\site-packages\PIL\ImageColor.py", line 133, in getcolor
    color, alpha = getrgb(color), 255
  File "\lib\site-packages\PIL\ImageColor.py", line 118, in getrgb
    raise ValueError(f"unknown color specifier: {repr(color)}")
ValueError: unknown color specifier: 'transparent'
artemox commented 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
sbuss commented 2 years ago

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')
artemox commented 2 years ago

Thanks!

Here's a working example with your radial color gradient: