How To Create qr code Generator in Python
You can create a QR code generator in Python using the qrcode
library. Here's a basic example:
import qrcode
data = "Hello, world!"
filename = "qrcode.png"
# Generate QR code
img = qrcode.make(data)
# Save QR code image
img.save(filename)
In this example, we import the qrcode
library and set the data
that we want to encode in the QR code to "Hello, world!".
We also specify the filename of the QR code image that we want to save.
We then generate the QR code image using the qrcode.make()
method, which
takes the data as its argument. This method returns a PIL image object
representing the QR code.
Finally, we save the image to a file using the img.save()
method,
which takes the filename as its argument.
You can customize the QR code by specifying additional options to the
qrcode.make()
method, such as the size and color of the QR code. Here's an example:
import qrcode
data = "Hello, world!"
filename = "qrcode.png"
# Set QR code options
qr = qrcode.QRCode(
version=1,
box_size=10,
border=5
)
# Add data to QR code
qr.add_data(data)
qr.make(fit=True)
# Generate QR code image
img = qr.make_image(fill_color="black", back_color="white")
# Save QR code image
img.save(filename)
In this example, we create a QRCode
object and set its version, box size, and border options. We then add the data to the QR code using the qr.add_data()
method and generate the QR code image using the qr.make_image()
method, which takes the fill color and background color as its arguments.
Finally, we save the image to a file using the img.save()
method as before.
Comments
Post a Comment