バイナリ PBM/PGM/PPM ファイルを作成する方法を理解しようとしています。私が知っているように、各形式にはプレーンと生の 2 つのタイプがあります。たとえば、黒の PBM 5x5 の構造は次のようになります。
P1
# This is a comment
5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
ご覧のとおり、単純です: 白は 0、黒は 1 です。ただし、PBM には生のバージョンがあり、次のようになります。
'P4\n# This is a comment\n5 5\n\xf8\xf8\xf8\xf8\xf8'
どうすればいいですか?PBM形式の説明は次のとおりです。
A raster of Height rows, in order from top to bottom. Each row is Width bits, packed 8 to a byte, with don't care bits to fill out the last byte in the row. Each bit represents a pixel: 1 is black, 0 is white. The order of the pixels is left to right. The order of their storage within each file byte is most significant bit to least significant bit. The order of the file bytes is from the beginning of the file toward the end of the file.
A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous.
何をする必要があるのか わかりません。struct
またはを使用する必要があるのではないかと思いますarray.array
が、よくわかりません。あなたの助けが必要です; そのようなファイルを作成する方法をPythonで例を挙げていただけますか?
>>> size = (5, 5)
>>> array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
>>> create_pbm(size, array)
'P4\n5 5\n\xf8\xf8\xf8\xf8\xf8'
より大きな画像 (2000x5000 など) を処理する必要があるため、適切な速度が必要です。ctypes
しかし、問題は、ライブラリなしで純粋な Python を使用する必要があることです。バイナリ PBM ファイルを作成する方法の簡単な例を教えてください。
バイナリの PGM と PPM の処理について教えていただけると、さらにすばらしいです。
ありがとう!