Last active 1 month ago

Revision f0deff32d5e90f9d5d3925cbb89e81822d2200e0

example.js Raw
example.js
text2json_qr.py Raw
1import qrcode, json
2
3def 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:
47print(json.dumps(generate_qr_boolean_array("Hello, world!")))