-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26. Simple QR Code Generator and Scanner.py
More file actions
36 lines (31 loc) · 1.05 KB
/
26. Simple QR Code Generator and Scanner.py
File metadata and controls
36 lines (31 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import qrcode
from pyzbar.pyzbar import decode
from PIL import Image
def generate_qr_code(data, filename):
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(filename)
print("QR Code generated successfully!")
def scan_qr_code(image_path):
try:
result = decode(Image.open(image_path))
print("Scanned data:", result[0].data.decode())
except FileNotFoundError:
print("Image not found.")
def main():
print("1. Generate QR Code")
print("2. Scan QR Code")
choice = input("Enter your choice: ")
if choice == '1':
data = input("Enter data to encode: ")
filename = input("Enter filename to save QR Code: ")
generate_qr_code(data, filename)
elif choice == '2':
image_path = input("Enter image path: ")
scan_qr_code(image_path)
else:
print("Invalid choice")
if __name__ == "__main__":
main()