Pythonスクリプトのexe化を試してみます。
Pyinstallerをインストール
powershellでpipを使ってpyinstallerパッケージをインストール
pip install pyinstaller動作確認
PS F:\python\pyinstaller> pyinstaller
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME] [--contents-directory CONTENTS_DIRECTORY]
                   [--add-data SOURCE:DEST] [--add-binary SOURCE:DEST] [-p DIR] [--hidden-import MODULENAME]
                   [--collect-submodules MODULENAME] [--collect-data MODULENAME] [--collect-binaries MODULENAME]
                   [--collect-all MODULENAME] [--copy-metadata PACKAGENAME] [--recursive-copy-metadata PACKAGENAME]
                   [--additional-hooks-dir HOOKSPATH] [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
                   [--splash IMAGE_FILE] [-d {all,imports,bootloader,noarchive}] [--optimize LEVEL]
                   [--python-option PYTHON_OPTION] [-s] [--noupx] [--upx-exclude FILE] [-c] [-w]
                   [--hide-console {minimize-early,hide-late,hide-early,minimize-late}]
                   [-i <FILE.ico or FILE.exe,ID or FILE.icns or Image or "NONE">] [--disable-windowed-traceback]
                   [--version-file FILE] [--manifest <FILE or XML>] [-m <FILE or XML>] [-r RESOURCE] [--uac-admin]
                   [--uac-uiaccess] [--argv-emulation] [--osx-bundle-identifier BUNDLE_IDENTIFIER]
                   [--target-architecture ARCH] [--codesign-identity IDENTITY] [--osx-entitlements-file FILENAME]
                   [--runtime-tmpdir PATH] [--bootloader-ignore-signals] [--distpath DIR] [--workpath WORKPATH] [-y]
                   [--upx-dir UPX_DIR] [--clean] [--log-level LEVEL]
                   scriptname [scriptname ...]
pyinstaller: error: the following arguments are required: scriptnameスクリプトをexe化してみる。
用意したpythonスクリプトはOpenCVを使ってカラーの画像ファイルをグレースケール化した画像ファイルに変換します。
sample01.py
import cv2
import sys
args = sys.argv
exe_file = args[0]
in_file = args[1]
out_file = args[2]
# 画像ファイルの読み込み
src = cv2.imread(in_file)
# グレースケール化
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# 画像ファイルの書き込み
cv2.imwrite(out_file, gray)
print("{0} {1} {2}".format(exe_file, in_file, out_file))pyinstallerを使ってexe化
 pyinstaller sample01.py
309 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
309 INFO: Python: 3.10.11
315 INFO: Platform: Windows-10-10.0.22631-SP0
~省略~
11236 INFO: Building COLLECT COLLECT-00.toc
11685 INFO: Building COLLECT COLLECT-00.toc completed successfully.exeファイルが出来上がっていることを確認
PS F:\python\pyinstaller> ls dist\sample01
    Directory: F:\python\pyinstaller\dist\sample01
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2024/08/24     8:28                _internal
-a---          2024/08/24     8:28        3289352 sample01.exe
exe化したプログラムを実行
PS F:\python\pyinstaller> dist\sample01\sample01.exe color.png gray.png
F:\python\pyinstaller\dist\sample01\sample01.exe color.png gray.pngグレースケール化した画像ファイルが出来上がっているいるので成功したようです。
一つのexeファイルにまとめる
onefileオプションでexeファイルに関連ファイルをまとめることが出来るようです。
PS F:\python\pyinstaller> pyinstaller sample01.py --onefile
309 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
310 INFO: Python: 3.10.11
316 INFO: Platform: Windows-10-10.0.22631-SP0
~省略~
18882 INFO: Fixing EXE headers
21168 INFO: Building EXE from EXE-00.toc completed successfully.PS F:\python\pyinstaller> ls dist
    Directory: F:\python\pyinstaller\dist
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2024/08/24     8:41       57618929 sample01.exeexeのファイルサイズがだいぶ大きくなっているので関連ファイルが一つにまとまっていると思います。
 
  
  
  
  


コメント