Last active 1 month ago

wowlikon's Avatar wowlikon revised this gist 1 month ago. Go to revision

No changes

wowlikon's Avatar wowlikon revised this gist 1 month ago. Go to revision

2 files changed, 165 insertions, 107 deletions

example.js

Diff is too large to be shown

text2json_qr.py

@@ -1,47 +1,28 @@
1 1 import qrcode, json
2 2
3 3 def generate_qr_boolean_array(text):
4 - """
5 - Generates a QR code for the given text using version 5 and error correction level Q,
6 - then converts it to a 2D boolean array where each element represents a module (square):
7 - - True for black (dark) modules
8 - - False for white (light) modules
9 -
10 - Note: QR version 5 has a fixed size of 37x37 modules (excluding borders).
11 - If the text is too long, it may not fit; adjust version or error correction as needed.
12 - """
13 - # Create QR code object with specified parameters
14 4 qr = qrcode.QRCode(
15 - version=5, # Fixed to version 5
16 - error_correction=qrcode.constants.ERROR_CORRECT_Q, # Q level error correction
17 - box_size=1, # Each module is 1 pixel (not relevant for boolean array)
18 - border=0, # No border to get raw 37x37 modules
5 + version=5,
6 + error_correction=qrcode.constants.ERROR_CORRECT_Q,
7 + box_size=1,
8 + border=0,
19 9 )
20 10
21 - # Add data and generate the QR code
22 11 qr.add_data(text)
23 - qr.make(fit=True) # This will attempt to fit, but version is fixed to 5
12 + qr.make(fit=True)
24 13
25 - # Generate the image (black and white)
26 14 img = qr.make_image(fill_color="black", back_color="white")
27 -
28 - # Get image dimensions (should be 37x37 for version 5 with border=0)
29 15 width, height = img.size
30 16
31 - # Convert to 2D boolean list
32 17 boolean_array = []
33 18 for y in range(height):
34 19 row = []
35 20 for x in range(width):
36 21 pixel = img.getpixel((x, y))
37 - # Check if pixel is black (0 for grayscale, or (0,0,0) for RGB)
38 - if pixel == 0 or pixel == (0, 0, 0):
39 - row.append(True) # Black module
40 - else:
41 - row.append(False) # White module
22 + if pixel == 0 or pixel == (0, 0, 0): row.append(True)
23 + else: row.append(False)
42 24 boolean_array.append(row)
43 25
44 26 return boolean_array
45 27
46 - # Example usage:
47 28 print(json.dumps(generate_qr_boolean_array("Hello, world!")))

wowlikon's Avatar wowlikon revised this gist 1 month ago. Go to revision

2 files changed, 246 insertions

example.js(file created)

Diff is too large to be shown

text2json_qr.py(file created)

@@ -0,0 +1,47 @@
1 + import qrcode, json
2 +
3 + def generate_qr_boolean_array(text):
4 + """
5 + Generates a QR code for the given text using version 5 and error correction level Q,
6 + then converts it to a 2D boolean array where each element represents a module (square):
7 + - True for black (dark) modules
8 + - False for white (light) modules
9 +
10 + Note: QR version 5 has a fixed size of 37x37 modules (excluding borders).
11 + If the text is too long, it may not fit; adjust version or error correction as needed.
12 + """
13 + # Create QR code object with specified parameters
14 + qr = qrcode.QRCode(
15 + version=5, # Fixed to version 5
16 + error_correction=qrcode.constants.ERROR_CORRECT_Q, # Q level error correction
17 + box_size=1, # Each module is 1 pixel (not relevant for boolean array)
18 + border=0, # No border to get raw 37x37 modules
19 + )
20 +
21 + # Add data and generate the QR code
22 + qr.add_data(text)
23 + qr.make(fit=True) # This will attempt to fit, but version is fixed to 5
24 +
25 + # Generate the image (black and white)
26 + img = qr.make_image(fill_color="black", back_color="white")
27 +
28 + # Get image dimensions (should be 37x37 for version 5 with border=0)
29 + width, height = img.size
30 +
31 + # Convert to 2D boolean list
32 + boolean_array = []
33 + for y in range(height):
34 + row = []
35 + for x in range(width):
36 + pixel = img.getpixel((x, y))
37 + # Check if pixel is black (0 for grayscale, or (0,0,0) for RGB)
38 + if pixel == 0 or pixel == (0, 0, 0):
39 + row.append(True) # Black module
40 + else:
41 + row.append(False) # White module
42 + boolean_array.append(row)
43 +
44 + return boolean_array
45 +
46 + # Example usage:
47 + print(json.dumps(generate_qr_boolean_array("Hello, world!")))
Newer Older