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

Question:

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/")
Asked By: RekWolf

||

Answers:

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)
Answered By: yvesonline

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)
Answered By: Jurakin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.