-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-ascii-qr-generator.py
57 lines (45 loc) · 1.75 KB
/
python-ascii-qr-generator.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import qrcode
from PIL import Image
def create_ascii_qr(data, size=1):
# Create QR code instance
qr = qrcode.QRCode(version=1, box_size=1, border=1)
# Add data to the QR code
qr.add_data(data)
qr.make(fit=True)
# Create an image from the QR code
img = qr.make_image(fill_color="black", back_color="white")
# Resize the image
img = img.resize((size * img.size[0], size * img.size[1]))
# Convert to ASCII
ascii_qr = ""
for y in range(0, img.size[1], 2):
for x in range(img.size[0]):
# Get two vertical pixels at a time
upper_pixel = img.getpixel((x, y))
lower_pixel = img.getpixel((x, y+1)) if y+1 < img.size[1] else 255
# Choose ASCII character based on the two pixels
if upper_pixel == 0 and lower_pixel == 0:
ascii_qr += "█"
elif upper_pixel == 0 and lower_pixel == 255:
ascii_qr += "▀"
elif upper_pixel == 255 and lower_pixel == 0:
ascii_qr += "▄"
else:
ascii_qr += " "
ascii_qr += "\n"
return ascii_qr
def main():
print("Compact ASCII QR Code Generator")
print("-------------------------------")
while True:
data = input("Enter the URL or text (or 'q' to quit): ")
if data.lower() == 'q':
break
size = input("Enter the size (default is 1): ")
size = int(size) if size.isdigit() else 1
ascii_qr = create_ascii_qr(data, size)
print("\nHere's your compact ASCII QR code:")
print(ascii_qr)
print(f"Data: {data}")
if __name__ == "__main__":
main()