LoginSignup
2
1

More than 1 year has passed since last update.

MacのpythonでGS1 Data Matrix を生成する方法

Posted at

GS1 Data Matrix(データマトリックス)を生成するのに手こずったのでメモ。

データマトリックス(バーコード)は以下のような画像になります。
製品の識別や追跡、情報の管理などに使用されます。
dmtx.png

現在の環境

  • 機種: M1 MacBook Pro
  • OS: MacOS Ventura
  • python: Python 3.10.4

ライブラリのインストール

こちらのページの手順に沿って行いました。
https://pypi.org/project/pylibdmtx/

Homebrewでlibmtxをインストール。

> brew install libdmtx

インストールを確認します。

> find /opt/homebrew -name libdmtx\*

/opt/homebrew/Cellar/libdmtx/0.7.7/include/dmtx.h
/opt/homebrew/Cellar/libdmtx/0.7.7/lib/libdmtx.0.dylib
/opt/homebrew/Cellar/libdmtx/0.7.7/lib/pkgconfig/libdmtx.pc
/opt/homebrew/Cellar/libdmtx/0.7.7/lib/ (2 other files)
/opt/homebrew/Cellar/libdmtx/0.7.7/share/man/man3/libdmtx.3

pythonのlibdmtxラッパーをインストール。

> pip install pylibdmtx

pythonのインタラクティブシェルに入ります。

> python3
Python 3.10.4 (main, Jul  8 2022, 12:41:13) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

上記参考ページのExample usageを実行します。
デコードが先に書いてありますが、今回はエンコードだけできたらいいので、エンコードは飛ばします。
ただしデコードのインポート部分は必要になります。
1行ずつコピペして実行します。

>>> from pylibdmtx.pylibdmtx import decode
>>> from pylibdmtx.pylibdmtx import encode
>>> from PIL import Image
>>> encoded = encode('hello world'.encode('utf8'))
>>> img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
>>> img.save('dmtx.png')
>>> print(decode(Image.open('dmtx.png')))
[Decoded(data=b'hello world', rect=Rect(left=9, top=10, width=80, height=79))]

と、すんなり動けば成功ですが、下記のエラーに悩まされました。

raise ImportError('Unable to find dmtx shared library')

色々検索してみたものの、改善されず。
試しに Intel MacBook Pro で試したらすんなり動くという謎……
その後も検索を続けて、たどり着いたページ。
https://github.com/dmtx/libdmtx/issues/31
ページの下の方にあるシンボリックリンクを貼るという方法で解決しました

sudo ln -s /opt/homebrew/lib/libdmtx.dylib /usr/local/lib/

以上、何かの参考になれば幸いです。

2
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
2
1