Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算图像(dpx)文件中的bytearray偏移量_Python_Image_File_Header_Bytearray - Fatal编程技术网

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

Python 计算图像(dpx)文件中的bytearray偏移量,python,image,file,header,bytearray,Python,Image,File,Header,Bytearray,我时不时地在读二进制数据。但我从来没有真正了解过系统在使用bytearray时如何推断偏移量 在本例中,我正在处理一个dpx文件,并试图更改位于方向标头中的aspectratio。 可在此处找到相关文件信息: 我知道斯科特·格里菲斯(Scott Griffiths)在这方面有一篇很好的文章。 然而,我从来没有真正理解到将这些知识转换为修改除GENERICFILEHEADER之外的其他标题下的内容 那么,如何改变aspectratio呢。在这件事上的任何帮助都将不胜感激 干杯有一个非常好的文档,名

我时不时地在读二进制数据。但我从来没有真正了解过系统在使用bytearray时如何推断偏移量

在本例中,我正在处理一个dpx文件,并试图更改位于方向标头中的aspectratio。 可在此处找到相关文件信息:

我知道斯科特·格里菲斯(Scott Griffiths)在这方面有一篇很好的文章。 然而,我从来没有真正理解到将这些知识转换为修改除GENERICFILEHEADER之外的其他标题下的内容

那么,如何改变aspectratio呢。在这件事上的任何帮助都将不胜感激


干杯

有一个非常好的文档,名为“数字运动图像交换的文件格式”,我想它可能会对您有所帮助。我不确定官方版本在哪里,但有一个版本是

无论如何,这里有一段代码片段,可以用来更改像素纵横比:

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之前的方向标头中有几个字段:XOffset、yoOffset、XCenter、YCenter、XOriginalSize、yorOriginalSize、文件名、时间日期、InputName、InputSN和边框。这些字段的字节长度分别为4、4、4、4、4、100、24、32、32和8;总共220人。AspectRatio的偏移量是这些字段的总和:768+640+220=1628


这是计算正确偏移量的困难方法。如果你只看上面列出的.pdf文件,就会容易得多。它列出了表中的所有字段偏移量:p

嘿,感谢您的快速回复。非常感谢您的代码片段。您是如何处理aspectratio的偏移量的。它说方向头有256字节长。我只是没有把它和1628的偏移量相匹配。太棒了。对二进制偏移量的这种洞察是非常棒的。谢谢你抽出时间。稍微更改了代码,因为Python2.6似乎不喜欢“rb”模式下的write命令将其更改为“rb+”,并且似乎工作正常