这两天拿到一批.seq文件无法打开,着实让我头疼了一把。通过分析二进制文件发现是以Normix开头,google之发现是一个高速摄像机采集软件,.seq应该是一些图像组成的序列,可以用自家的StreamPix软件打开。
于是我首先搜索这个软件,发现不是无法下载就是假文件链接。接着想如果是图像序列,能不能直接把二进制代码分割开,每段存成一张图像呢?那么问题来了,如何知道从哪儿分割呢?
我先用ultraEdit打开一张jpg文件,发现是以FF D8 FF E0 00 10 4A 46 49 46这几个二进制字符开始的,再打开一张,还是这几个字符。于是我就大胆猜测,是不是所有jpg文件都是这样的呢?如果.seq文件里有很多这个字符串,是不是就能认为它是由一系列jpg文件组合成的呢?赶紧在.seq的二进制代码里面搜,哈,果然有很多这个字符串呢!
好了,下面问题就是打开.seq,以该字符串分割它,并把每个片段存成一个jpg。第一个分割片段是.seq头部可以忽略不计。以下是python代码。我把每个.seq文件都转存为从1.jpg开始编号的jpg文件序列,存放在.seq文件同名的文件夹中。代码运行良好。
# Deal with .seq format for video sequence
# Author: Kaij
# The .seq file is combined with images,
# so I split the file into several images with the image prefix
# "\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46".
import os.path
import fnmatch
import shutil
def open_save(file,savepath):
# read .seq file, and save the images into the savepath
f = open(file,'rb')
string = str(f.read())
splitstring = "\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46"
# split .seq file into segment with the image prefix
strlist=string.split(splitstring)
f.close()
count = 0
# delete the image folder path if it exists
if os.path.exists(savepath):
shutil.rmtree(savepath)
# create the image folder path
if not os.path.exists(savepath):
os.mkdir(savepath)
# deal with file segment, every segment is an image except the first one
for img in strlist:
filename = str(count)+'.jpg'
filenamewithpath=os.path.join(savepath, filename)
# abandon the first one, which is filled with .seq header
if count > 0:
i=open(filenamewithpath,'wb+')
i.write(splitstring)
i.write(img)
i.close()
count += 1
if __name__=="__main__":
rootdir = "D:\\Data\\feifei"
# walk in the rootdir, take down the .seq filename and filepath
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
# check .seq file with suffix
if fnmatch.fnmatch(filename,'*.seq'):
# take down the filename with path of .seq file
thefilename = os.path.join(parent, filename)
# create the image folder by combining .seq file path with .seq filename
thesavepath = parent+'\\'+filename.split('.')[0]
print "Filename=" + thefilename
print "Savepath=" + thesavepath
open_save(thefilename,thesavepath)

本文介绍了如何使用Python将以Normix开头的.seq文件转换为.jpg图像序列。作者发现.seq文件包含多个以特定二进制字符串开头的jpg片段,通过查找并分割这些字符串,成功将.seq文件拆分为单独的jpg图片。



最低0.47元/天 解锁文章
1582

被折叠的 条评论
为什么被折叠?



