Skip to content

Instantly share code, notes, and snippets.

@r0lodex
Created August 25, 2025 22:47
Show Gist options
  • Select an option

  • Save r0lodex/54546aab7694b40c0c8519cf7c06eb7a to your computer and use it in GitHub Desktop.

Select an option

Save r0lodex/54546aab7694b40c0c8519cf7c06eb7a to your computer and use it in GitHub Desktop.
QR Reader from Byte Array

Read QR from Byte Array

The code below will read byte array saved in a txt file, outputs the qr string and saves the qr image, like so

python readqr.py bytearray_in_text_file.txt

Usage

  • Save the python script below
  • python -m venv env
  • source env/bin/activate
  • pip install pillow opencv-python
  • If all good, just use it like the example above.
import argparse
import io
import ast
from pathlib import Path
from PIL import Image
import cv2
import numpy as np

def main():
    parser = argparse.ArgumentParser(description="Decode QR from byte array in txt file")
    parser.add_argument("txtfile", help="Path to txt file containing byte array")
    args = parser.parse_args()

    txt_path = Path(args.txtfile)

    with open(txt_path, "r") as f:
        content = f.read().strip()

    byte_array = ast.literal_eval(content)

    byte_data = bytes((b + 256) % 256 for b in byte_array)

    png_path = txt_path.with_suffix(".png")

    image = Image.open(io.BytesIO(byte_data))
    image.save(png_path)
    print(f"Saved as {png_path}")

    img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    detector = cv2.QRCodeDetector()
    qr_data, _, _ = detector.detectAndDecode(img_cv)

    if qr_data:
        print("QR Payload:", qr_data)
    else:
        print("No QR code")

if __name__ == "__main__":
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment