SingleImage (PNG保存) プロジェクト(GbE-2 / Python)
This content is not available in your language yet.
HelloWorld で作成した共通モジュール puclib.py を使用して、GbE-2 から 1 フレームの圧縮画像データを取得・復号し、PNG 画像形式 で保存するスクリプトの作成手順です。
-
スクリプトの作成
puclib.pyと同じフォルダーにgbe2_single_image_png.pyを作成し、以下のコードを記述します。gbe2_single_image_png.py from ctypes import c_uint32, c_ushort, c_ubyte, c_void_p, byrefimport numpy as npimport cv2from PIL import Image# 共通モジュール puclib.py から読み込みfrom puclib import (load_sdk, PUC_SUCCEEDED, PUC_DETECT_TARGET_HAWK,PUC_Q_COUNT, PUC_DETECT_FILTER, PUC_DETECT_INFO, PUC_XFER_DATA_INFO)def main():print("INFINICAM GbE-2 SingleImage (PNG) キャプチャ開始...")# 1. SDKのロードと初期化puclib = load_sdk()if puclib.PUC_Initialize() != PUC_SUCCEEDED:print("エラー: SDKの初期化に失敗しました。")return# 2. デバイスの検索とオープンdetect_filter = PUC_DETECT_FILTER(nDeviceTypeFlags=PUC_DETECT_TARGET_HAWK)detect_info = PUC_DETECT_INFO()if puclib.PUC_DetectDevice2(byref(detect_filter), byref(detect_info)) != PUC_SUCCEEDED or detect_info.nDeviceCount == 0:print("エラー: GbE-2 カメラが見つかりませんでした。")returnh_device = c_void_p()if puclib.PUC_OpenDevice2(0, byref(h_device)) != PUC_SUCCEEDED:print("エラー: デバイスのオープンに失敗しました。")return# 3. カメラパラメータの取得width, height = c_uint32(), c_uint32()puclib.PUC_GetResolution(h_device, byref(width), byref(height))w, h = width.value, height.valueq_vals = (c_ushort * PUC_Q_COUNT)()puclib.PUC_GetQuantization2(h_device, q_vals)xfer_data_size = c_uint32()puclib.PUC_GetXferDataSize(h_device, byref(xfer_data_size))print(f"解像度: {w} x {h} ピクセル")# 4. バッファの確保comp_buffer = (c_ubyte * xfer_data_size.value)()line_bytes = (w + 3) & ~3 # 4バイト境界アラインメントdecoded_buffer = (c_ubyte * (line_bytes * h))()# 5. キャプチャの実行xfer_info = PUC_XFER_DATA_INFO()xfer_info.pData = comp_bufferxfer_info.nDataSize = xfer_data_size.valueprint("1フレームのキャプチャを実行中...")if puclib.PUC_GetSingleXferData(h_device, byref(xfer_info)) == PUC_SUCCEEDED:# 6. 復号処理ret = puclib.PUC_DecodeData(decoded_buffer, 0, 0, w, h, line_bytes, comp_buffer, q_vals)if ret == PUC_SUCCEEDED:# 7. ctypes バッファを NumPy 配列へ変換 (パディングを除去)img_np = np.ctypeslib.as_array(decoded_buffer).reshape((h, line_bytes))[:, :w]# 方法 A: OpenCV を使用して PNG 保存cv2.imwrite("captured_image_cv2.png", img_np)print("SUCCESS: OpenCV経由で captured_image_cv2.png を保存しました。")# 方法 B: Pillow (PIL) を使用して PNG 保存img_pil = Image.fromarray(img_np)img_pil.save("captured_image_pil.png")print("SUCCESS: Pillow経由で captured_image_pil.png を保存しました。")else:print(f"エラー: 画像データの復号に失敗しました。ErrorCode: {ret}")else:print("エラー: 画像データの取得に失敗しました。")# 8. クローズpuclib.PUC_CloseDevice(h_device)if __name__ == "__main__":main() -
スクリプトの実行
Terminal window python gbe2_single_image_png.py実行完了後、カレントディレクトリに
captured_image_cv2.pngおよびcaptured_image_pil.pngが生成されます。
PNG 保存手法の比較
Section titled “PNG 保存手法の比較”OpenCV (cv2.imwrite)
Section titled “OpenCV (cv2.imwrite)”復号結果の NumPy 配列(2次元 uint8 型)を直接指定してエンコード・保存します。画像処理パイプラインの途中でそのまま保存したい場合に最適です。
cv2.imwrite("captured_image_cv2.png", img_np)Pillow (Image.fromarray)
Section titled “Pillow (Image.fromarray)”NumPy 配列を PIL の Image オブジェクトへ変換してから出力します。圧縮率の調整やメタデータの付与を行いたい場合に便利です。
img_pil = Image.fromarray(img_np)img_pil.save("captured_image_pil.png")