python转存.seq文件为.jpg序列

python转存.seq文件为.jpg序列

这两天拿到一批.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是一种动态解释型的编程语言。Python可以在Windows、UNIX、MAC等多种操作系统上使用,也可以在Java、.NET开发平台上使用。

【特点】
1 Python使用C语言开发,但是Python不再有C语言中的指针等复杂的数据类型。
2 Python具有很强的面向对象特性,而且简化了面向对象的实现。它消除了保护类型、抽象类、接口等面向对象的元素。
3 Python代码块使用空格或制表符缩进的方式分隔代码。
4 Python仅有31个保留字,而且没有分号、begin、end等标记。
5 Python是强类型语言,变量创建后会对应一种数据类型,出现在统一表达式中的不同类型的变量需要做类型转换。

【搭建开发环境】
1 可以到www.python.org下载安装包,然后通过configure、make、make install进行安装。
2 也可以到www.activestate.com去下载ActivePython组件包。(ActivePython是对Python核心和常用模块的二进制包装,它是ActiveState公司发布的Python开发环境。ActivePython使得Python的安装更加容易,并且可以应用在各种操作系统上。ActivePython包含了一些常用的Python扩展,以及Windows环境的编程接口)。对ActivePython来说,如果你是windows用户,下载msi包安装即可;如果你是Unix用户,下载tar.gz包直接解压即可。
3 Python的IDE,包括PythonWin、Eclipse+PyDev插件、Komodo、EditPlus

【版本】
python2与python3是目前主要的两个版本。
如下两种情况下,建议使用python2:
1 你无法完全控制你即将部署的环境时;
2 你需要使用一些特定的第三方包或扩展时;
python3是官方推荐的且是未来全力支持的版本,目前很多功能提升仅在python3版本上进行。

【hello world】
1 创建hello.py
2 编写程序:
Hello world代码 复制代码 收藏代码
  1. if __name__ == \'__main__\':
  2. print "hello word"


3 运行程序:
Java代码 复制代码 收藏代码
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值