0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでQRコードの読み取り

Posted at

PythonでQRコードの読み取り

コード

qr.py
import sys
import cv2
from pyzbar.pyzbar import decode

def read_qr_from_png(png_path):
    img = cv2.imread(png_path)
    if img is None:
        print("画像が読み込めませんでした。")
        return
    qr_codes = decode(img)
    if not qr_codes:
        print("QRコードが見つかりませんでした。")
        return
    for qr in qr_codes:
        print(qr.data.decode('utf-8'))

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("使い方: python qr_reader.py <画像ファイルパス>")
        sys.exit(1)
    read_qr_from_png(sys.argv[1])

動作確認した環境

Windows 11、Python 3.13.7で動作確認。

使い方

① 事前に必要なパッケージ(opencv-pythonpyzbar)をインストールする

pip install opencv-python pyzbar

② 実行して以下のエラーが出た場合、Microsoft公式から、Viaual C++ 2013 再配布パッケージをダウンロード&インストールする(参考記事: pyzbarでlibiconv.dllが見つからないエラー - Qiita )。

FileNotFoundError: Could not find module 
'C:\Users\(ユーザー名)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\pyzbar\libzbar-64.dll' (or one of its dependencies). Try using the full path with constructor syntax.

表記

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

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?