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

标签 python image python-imaging-library

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

这是我目前执行此操作的代码:

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

最佳答案

这可能不是最Pythonic或最快的方法(不使用结构或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 年),因为它列出了 header 中其他好东西的所有偏移量。

关于python - 在 Python 中检索 .dpx 文件的分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854873/

相关文章:

python - 你如何解决 pyinstaller for scipy 中的 'hidden imports not found!' 警告?

html - 容器的图像 100% 高度 - 最小高度在 Chrome/Safari 上的行为不同

python-3.x - 如何使用 PIL 将二值图像转换为 RGB?

python - 是否可以将参数解包运算符(又名飞溅运算符)实现为函数?

python - 如何将字典保存到文件中,并保持良好的格式?

C# - 图像比较(快速)

图像梯度角计算

python - 如何完成这个python函数保存在同一个文件夹中?

image - Strapi:通过重命名默认图像名称来自定义图像管道

python - MySQL 拒绝远程连接