Home  >  Article  >  Backend Development  >  python实现的jpg格式图片修复代码

python实现的jpg格式图片修复代码

WBOY
WBOYOriginal
2016-06-10 15:14:442624browse

最近为客户修复损坏的jpg写的,效果还可以,但不保证适用任何情况。

如果你有损坏照片,不妨试一试,如果可以使用给我留个言哦。

复制代码 代码如下:

# -*- coding: utf8 -*-
# !/usr/bin/env python

__author__ = 'fengxing'
__date__ = '2012-1-18 20:13'

import sys


def jpgfix(name):
    sig = '\xFF\xD8\xFF\xDB'
    with open(name, "r") as fd:
        fd.seek(len(sig), 0)
        jpg = fd.read()
        pos = jpg.find(sig)
        if pos             raise Exception('Not find signature')
        jpg = jpg[pos:]

    with open(name, "w") as fd:
        fd.seek(0, 0)
        print 'size is:', len(jpg)
        fd.write(jpg)

if __name__ == '__main__':
    try:
        while True:
            jpgfix(sys.argv[1])
    except:
        print 'Done'

使用方法很简单,上面代码另存为jpg_fix.py,再从命令行里输入

复制代码 代码如下:

python jpg_fix.py ABC.jpg

ABC.jpg就是你要修复的图片(注意:修复前请自行备份)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn