-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqrcode_reader.py
More file actions
40 lines (31 loc) · 1.28 KB
/
qrcode_reader.py
File metadata and controls
40 lines (31 loc) · 1.28 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
37
38
39
import os
import sys
# Function to read a single QR code using zbarimg
def read_qr_code(image_path, save_to_file=False, output_file=None):
# Check if the image file exists
if not os.path.isfile(image_path):
print(f"Error: File '{image_path}' does not exist.")
return
# Run zbarimg to decode the QR code
result = os.popen(f'zbarimg --quiet --raw {image_path}').read().strip()
if result:
print("QR Code Data:", result)
# Save to file if specified
if save_to_file and output_file:
with open(output_file, 'w') as file:
file.write(result + '\n')
print(f"QR code data saved to {output_file}")
else:
print("No QR code found or the image is unreadable.")
# Command-line interface for the script
if __name__ == '__main__':
# Check for image path argument
if len(sys.argv) < 2:
print("Usage: python qr_reader.py <image_path> [output_file]")
sys.exit(1)
# Get the image path from the first argument
image_path = sys.argv[1]
# Optional: Output file to save QR code data
output_file = sys.argv[2] if len(sys.argv) > 2 else None
# Call the function to read the QR code
read_qr_code(image_path, save_to_file=bool(output_file), output_file=output_file)