私はバイナリPBM形式で作業しています。それを読むと、整数の配列があります。整数はバイトの序数です。配列内の各整数は、バイナリ表現として0と1の整数のリストに変換され、次にこのリストを逆にします。ピクセルグリッドは0:0から始まるため、最初のピクセルの位置は[0:0]です。
x> = 8の場合、ピクセルカラーを取得する必要があります。x<8の場合、すべてがうまく機能します。ピクセルカラーを取得するためのコード。
def getpixel(self, x, y):
'''PNMReader.getpixel(x, y) -> int
Get pixel at coordinates [x:y] and return it as integer.'''
if not isinstance(x, int) or not isinstance(y, int):
raise(TypeError('both x and y must be integers'))
if x < -1 or y < -1:
raise(ValueError('both x and y are interpreted as in slice notation'))
if x > (self.width-1):
raise(ValueError('x cannot be equal or greater than width'))
if y > (self.height-1):
raise(ValueError('x cannot be equal or greater than height'))
width, height = self.width, self.height
x = (x, width-1)[x == -1]
y = [y, height-1][y == -1]
p = (y *height) +x
width, height = self.width, self.height
pixels = self._array_
q = (8, width)[width -8 < 0]
if x >= q:
while x % q:
y += 1
x -= 1
from pprint import pprint
color = bitarray(pixels[y])[::-1][:q][x]
print(color)
bitarray
ここで確認できるのは、整数のビットをリストとして取得するための私の定義済み関数です。self._array_
整数のシーケンスです(これは、PBMから読み取られたバイトの単なる序数です)。
x> = 8の場合、ピクセルカラーを取得するためにこの関数を修正する必要があります。このような状況でxとyのオフセットを計算する方法がわかりません。
迅速に機能する回答のみが受け入れられます。画像が大きい場合(たとえば、3000x5000ピクセルの場合)、速度が遅くなる可能性があるため、すべてのビットを1次元配列として結合する必要はありません。
imagemagick
やなどのモジュールを使用できることは知っていますがfreeimage
、使用できるのは標準ライブラリのみです(追加のモジュールは使用できません)。バインディングやデフォルト以外のモジュールのない純粋なPythonソリューションが必要です。