2016-06-16 117 views
1

現在、入力されたフォルダを検索し、ファイルの欠落や空のエラーを報告するプログラムを作成しています。私がチェックする必要があるエラーの1つは、すべての.dpxイメージが同じ解像度を持つかどうかです。しかし、私はこれを確認する方法を見つけることができないようです。 PILはファイルを開くことができず、メタデータを確認する方法が見つかりません。何か案は?Pythonで.dpxファイルの解像度を取得する

これは、現時点でこれを行うために、私が持っているコードです:

im = Image.open(fullName) 

if im.size != checkResolution: 
    numErrors += 1 
    reportMessages.append(ReportEntry(file, "WARNING", 
             "Unusual Resolution")) 

のfullNameは、ファイルへのパスです。 checkResolutionはタプルとしての正しい解像度です。 reportMessagesは、後でレポートに出力されるエラー文字列を単に収集します。現在のところプログラムを実行すると:

Traceback (most recent call last): 

    File "Program1V4", line 169, in <module> 
    main(sys.argv[1:]) 

    File "Program1V4", line 108, in main 
    im = Image.open(fullName) 

    File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1983, in open 
    raise IOError("cannot identify image file") 

IOError: cannot identify image file 

答えて

0

残念ながら、枕/ PILはまだSMPTEデジタル画像交換フォーマットを理解していません。

ただし、ImageMagick supportsとImageMagickはcontrolled by Pythonになります。単にImageMagickをas an external commandと呼びます。

さらに多くの作業が可能ですが、C libraryをコンパイルしてPythonから呼び出してください。 ImageMagickがこのライブラリをフードの下で使用しているのか、それとも独自の標準を実装しているのかを知ることは面白いでしょう。

2

これは、構造体やctypesを使用しないで、またはC言語で行うのではなく、最もpythonicな方法ではありませんが、ファイルヘッダから直接フィールドを取得します(エラーをチェックすることを忘れないでください)。 ...):

# Open the DPX file 
fi = open(frame, 'r+b') 

# Retrieve the magic number from the file header - this idicates the endianness 
# of the numerical file data 
magic_number = struct.unpack('I', currFile.read(4))[0] 

# Set the endianness for reading the values in 
if not magic_number == 1481655379: # 'SDPX' in ASCII 
    endianness = "<" 
else: 
    endianness = ">" 

# Seek to x/y offset in header (1424 bytes in is the x pixel 
# count of the first image element, 1428 is the y count) 
currFile.seek(1424, 0) 

# Retrieve values (4 bytes each) from file header offset 
# according to file endianness 
x_resolution = struct.unpack(endianness+"I", currFile.read(4))[0] 
y_resolution = struct.unpack(endianness+"I", currFile.read(4))[0] 

fi.close() 

# Put the values into a tuple 
image_resolution = (x_resolution, y_resolution) 

# Print 
print(image_resolution) 

DPXは、複数の画像要素がある場合は非常に困難なフォーマットが解析することで可能性を秘めている - 上記のコードは、あなたが最もユースケースのために探しているものを使用する(単一のイメージ要素を与える必要があります)大きな古いライブラリをインポートするオーバーヘッドはありません。

DPXのSMPTE規格を取得し、ヘッダー内に保持されている他のグッズのすべてのオフセットをリストするので、スキム(2014年の最後のリビジョン)を与える価値があります。

関連する問題