在Python中检索.dpx文件的分辨率

2024-05-16 13:15:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在编写一个程序,它可以搜索输入的文件夹并标记错误,如丢失或空文件。我需要检查的一个错误是所有的.dpx图像是否具有相同的分辨率。然而,我似乎找不到方法来检查这个。PIL无法打开文件,我也无法找到检查元数据的方法。有什么想法吗?在

这是我目前做这件事的准则:

im = Image.open(fullName)

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

fullName是文件的路径。checkResolution是作为元组的正确分辨率。reportMessages只是收集错误字符串以便稍后在报告中打印。此时运行程序将返回:

^{pr2}$

Tags: 文件数据方法标记图像程序文件夹pil
2条回答

不幸的是,Pill/PIL还不了解SMPTE数字图像交换格式。在

但是,ImageMagick supports它和ImageMagick可以是controlled by Python,或者您只需调用ImageMagick as an external command。在

还有一点工作是编译一个C library,然后从Python调用它。想知道ImageMagick是在暗中使用这个库,还是有自己的标准实现,这是很有趣的。在

这可能不是最python或最快的方法(不使用struct或ctypes,或者用c!),但我会直接从文件头中提取字段(别忘了检查错误…):

# 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年),因为它列出了标题中包含的其他优点的所有偏移量。在

相关问题 更多 >