计算图像(dpx)文件中的bytearray偏移量

时间:2012-11-04 01:05:24

标签: python image file header bytearray

我一直在努力阅读二进制数据。但是,在使用bytearray时,我从来没有真正理解如何推断偏移量。

在这种情况下,我正在处理dpx文件并尝试更改位于方向标题中的aspectratio。 在此处找到相关文件信息:http://www.fileformat.info/format/dpx/egff.htm

我知道Scott Griffiths在这个主题上有一篇很棒的帖子。Decode image file to extract image header information and modify it (with python)  但是,我从来没有真正理解将这些知识转换为修改其他标题下的内容而不是GENERICFILEHEADER

那么如何才能改变方面。任何有关此事的帮助将不胜感激。

干杯

1 个答案:

答案 0 :(得分:1)

有一个非常好的文档称为“数字移动图片交换的文件格式”,我认为可能会帮助你。我不确定官方版本的位置,但有一个版本是here

无论如何,这是一个可用于更改像素长宽比的代码片段:

import struct

fp = open('file.dpx', 'r+b')
fp.seek(1628) #Set the offset to the pixel aspect ratio field

#Prints out the current pixel aspect ratio. 
#Assumes big-endian -- Check the magic number for your use case
print struct.unpack_from('>I', fp.read(4))[0] #horizontal pixel aspect ratio
print struct.unpack_from('>I', fp.read(4))[0] #vertical pixel aspect ratio

#Change the aspect ratios to new values.  Again assumes big-endian
fp.seek(1628) #Return to the proper offset for aspect ratio
new_horizontal = struct.pack('>I', 4L) 
new_vertical = struct.pack('>I', 3L) 
fp.write(new_horizontal) #set the new horizontal pixel aspect ratio to 4
fp.write(new_vertical) #set the new vertical aspect ratio to 3
fp.close()

此代码假定您尚未阅读“文件头”和“图像头”。文件头是768字节,图像头是640字节。然后在AspectRatio之前的Orientation Header中有几个字段:XOffset,YOffset,XCenter,YCenter,XOriginalSize,YOriginalSize,FileName,TimeDate,InputName,InputSN和Border。这些字段的字节长度分别为4,4,4,4,4,4,100,24,32,32和8;总计为220. AspectRatio的偏移量是这些字段的总和:768 + 640 + 220 = 1628。

这是找出适当偏移量的难点。如果你只看一下上面列出的.pdf就容易多了。它列出了表中的所有字段偏移:p