eightban's memo

残しておきたい記事をまとめてみました。このブログに書いてあるドキュメントやブログで配布しているファイルの使用によって発生するいかなる損害に対してもこのブログの管理者は責任を負いません。使用する場合は自己責任のもとに使用してください。

Python

QRコードを大量に作るPythonプログラム

投稿日:

インストール

pip install qrcode
pip install pillow

コード

# -*- coding: utf-8 -*-
import qrcode
from qrcode.image.styledpil import StyledPilImage
#from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
#from qrcode.image.styles.colormasks import RadialGradiantColorMask
from qrcode.image.styles import colormasks, moduledrawers

#img_1 = qr.make_image(image_factory=StyledPilImage, module_drawer=RoundedModuleDrawer())
#img_2 = qr.make_image(image_factory=StyledPilImage, color_mask=RadialGradiantColorMask())
#img_3 = qr.make_image(image_factory=StyledPilImage, embeded_image_path="/path/to/image.png")
def generate_qr_code(data, filename):
    qr = qrcode.QRCode(
        version=1,
        #version=40,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        #error_correction=qrcode.constants.ERROR_CORRECT_M,
        #error_correction=qrcode.constants.ERROR_CORRECT_Q,
        #error_correction=qrcode.constants.ERROR_CORRECT_H,
        #mask_pattern=3,
        box_size=20,
        border=2,
    )
    qr.add_data(data.encode('utf-8'))  # 日本語文字列をUTF-8エンコーディングで処理
    #qr.make(fit=True)
    img = qr.make_image(image_factory=StyledPilImage, 
                        #
                        module_drawer=moduledrawers.SquareModuleDrawer()
                        #module_drawer=moduledrawers.RoundedModuleDrawer()
                        #module_drawer=moduledrawers.CircleModuleDrawer()
                        #module_drawer=moduledrawers.GappedSquareModuleDrawer()
                        #module_drawer=moduledrawers.HorizontalBarsDrawer()
                        #module_drawer=moduledrawers.VerticalBarsDrawer()
                        
                        )

    #img = qr.make_image(fill_color="black", back_color="white")
    img.save(filename)

def read_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:  # ファイルをUTF-8エンコーディングで読み込み
        return file.readlines()

def main():
    file_path = "a.txt"  # 入力ファイル名を固定化
    lines = read_file(file_path)

    for index, line in enumerate(lines):
        line = line.strip()
        if line:
            output_filename = f"dat/"+(str(index).zfill(3))+f".png"  # 出力ファイル名を固定化
            generate_qr_code(line, output_filename)
            print(f"QR code '{output_filename}' generated successfully!")

if __name__ == '__main__':
    main()

パラメーター

細かい設定はサイトにて

https://github.com/lincolnloop/python-qrcode

QRコードの商標はデンソーウェーブの登録商標です。

-Python

Copyright© eightban's memo , 2024 All Rights Reserved Powered by STINGER.