lincolnloop / python-qrcode

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

Using SVG and svg moduledrawers without PIL #262

Closed bridger closed 2 years ago

bridger commented 2 years ago

I'm excited to see that there are now SVG module drawers! I upgraded from v6.1 to try them out. Previously I was able to use the SVG QR code without installing Pillow, but this time I wasn't able to import qrcode without installing Pillow as well. Is it still possible to use this package without the PIL dependency?

For reference, this is the little bit of Flask code I'm using to render the SVG with the new circle drawer.

from flask import send_file

import qrcode
import qrcode.image.svg
from qrcode.image.styles.moduledrawers.svg import SvgPathCircleDrawer
import io

def svgQRCode(data):
  qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L, image_factory=qrcode.image.svg.SvgPathImage)
  qr.add_data(data)

  img = qr.make_image(module_drawer=SvgPathCircleDrawer())

  fileIO = io.BytesIO()
  img.save(fileIO)
  fileIO.seek(0)
  return send_file(fileIO, mimetype='image/svg+xml')
SmileyChris commented 2 years ago

It should be possible. Looking at it now...

bridger commented 2 years ago

It might be because of the way I'm installing qrcode. The latest code hasn't been released, so I'm installing by running:

pip install git+https://github.com/lincolnloop/python-qrcode@49060c484ce6d

Or, maybe that is a red herring... Here is the error I'm getting when importing qrcode:

>>> import qrcode
Traceback (most recent call last):
  File "/opt/venv/src/qrcode/qrcode/image/styles/moduledrawers/pil.py", line 11, in <module>
    from PIL import Image, ImageDraw
ModuleNotFoundError: No module named 'PIL'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/venv/src/qrcode/qrcode/__init__.py", line 1, in <module>
    from qrcode.main import QRCode
  File "/opt/venv/src/qrcode/qrcode/main.py", line 16, in <module>
    from qrcode.image.base import BaseImage
  File "/opt/venv/src/qrcode/qrcode/image/base.py", line 4, in <module>
    from qrcode.image.styles.moduledrawers.base import QRModuleDrawer
  File "/opt/venv/src/qrcode/qrcode/image/styles/moduledrawers/__init__.py", line 2, in <module>
    from .pil import CircleModuleDrawer  # noqa: F401
  File "/opt/venv/src/qrcode/qrcode/image/styles/moduledrawers/pil.py", line 13, in <module>
    import Image
ModuleNotFoundError: No module named 'Image'
SmileyChris commented 2 years ago

It's not, it's because of the backwards compatibility work I put into moduledrawers. I have a fix I'm pushing now..