Skip to content

Instantly share code, notes, and snippets.

@HarryZ10
Last active March 31, 2024 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarryZ10/5f887c3ab65dd36762ac4c3f7b273aec to your computer and use it in GitHub Desktop.
Save HarryZ10/5f887c3ab65dd36762ac4c3f7b273aec to your computer and use it in GitHub Desktop.
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