how to generate qr code with python and when scanned make it open a url defined?
Asked Answered
S

2

7

How do I generate a qr code which when scanned opens a url? is it possible to use a library like qrcode or pyqrcode to accomplish this?

something like this :

pyq = QRCode()
pyq.generate(url="http://google.com/")
Serotherapy answered 17/3, 2021 at 11:26 Comment(0)
I
15

Yes you can use qrcode:

import qrcode
import qrcode.image.svg

img = qrcode.make('http://www.google.com/', image_factory=qrcode.image.svg.SvgImage)

with open('qr.svg', 'wb') as qr:
    img.save(qr)
Ignition answered 17/3, 2021 at 11:40 Comment(2)
will this open. a. url if the user scans the code?Serotherapy
This depends on the QR code reader software the user uses to scan the QR code, some readers automatically open the URL some just display it and the user needs to do an additional click. Don't forget that the QR code just encodes information and doesn't store any action associated to/with it.Ignition
S
2

You can use google apis to create qr code without additional library:

import requests

WIDTH = 400
HEIGHT = 400

DATA = "http://www.google.com/"

image = requests.get(f"https://chart.googleapis.com/chart?chs={WIDTH}x{HEIGHT}&cht=qr&chl={DATA}")
image.raise_for_status()

with open("qr.png", "wb") as qr:
    qr.write(image.content)
Scathing answered 19/2, 2023 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.