Last active
March 31, 2024 21:11
-
-
Save HarryZ10/5f887c3ab65dd36762ac4c3f7b273aec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from pyzbar.pyzbar import decode | |
from PIL import Image | |
def decode_qrcodes(directory_path="./png", output_file="output.txt"): | |
concatenated_data, max_data_length = '', 0 | |
# Get all PNG files in the directory and sort them | |
sorted_files = sorted( | |
[filename for filename in os.listdir(directory_path)], | |
key=lambda x: x.split('-')[1].split('.')[0] | |
) | |
# Decode each QR code and concatenate the data | |
for filename in sorted_files: | |
file_path = os.path.join(directory_path, filename) | |
decoded_data = decode(Image.open(file_path)) | |
if decoded_data: | |
data = decoded_data[0].data.decode('utf-8') | |
concatenated_data += data | |
max_data_length = max(max_data_length, len(data)) | |
with open(output_file, 'w', encoding='utf-8') as file: | |
file.write(concatenated_data) | |
return max_data_length | |
print(f"Q2 Answer: {decode_qrcodes()} bytes") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment