CivArchive
    ← All articles
    Published August 21, 2023by cosciblog

    Python script to check qr codes

    102 views4 reactions0 comments on CivitAI0 collected
    helper_scriptqrcodetool guidepythonqrhelper

    After trying to generate some QR Codes with Stable Diffusion and checking them manualy with my smartphone, if they work or not, i created a python script with chat gpt where you only have to add an input, output folder and the value (URL) from your QR Code.

    After starting the script, it takes around a minute for 2000 images to scan, and copys all working qrcodes in the output folder.

    Python dependencies

    pip install pillow pyzbar

    Python script

    import os
    from PIL import Image
    from pyzbar.pyzbar import decode
    
    # Input folder with source images
    input_folder = r'C:\Users\cosci\Daten\dev\python\sd-qr-checker\in'
    
    # QRCode Value, for example a Domain https://www.cosci.de
    target_value = 'qrcode-value'
    
    # Output folder where working qrcodes will be copied
    output_folder = r'C:\Users\cosci\Daten\dev\python\sd-qr-checker\out'
    
    # File Blacklist, for example Grid files
    blacklist_filenames = ['grid']
    
    # Check if output folder exists
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # Whitelist of supported File extensions
    supported_extensions = ['.jpg', '.jpeg', '.png']
    
    # generate file list and check qr codes
    for filename in os.listdir(input_folder):
        if any(filename.lower().endswith(ext) for ext in supported_extensions) and all(item not in filename.lower() for item in blacklist_filenames):
            file_path = os.path.join(input_folder, filename)
            
            # decode qr codes
            image = Image.open(file_path)
            decoded_objects = decode(image)
            
            # check qr code
            for obj in decoded_objects:
                qr_data = obj.data.decode('utf-8')
                if qr_data == target_value:
                    output_path = os.path.join(output_folder, filename)
                    # copy qr code
                    image.save(output_path)
                    print(f"Success: {filename} copied.")
                    break