Created
February 9, 2016 07:57
-
-
Save annidy/8c0efc67d740eee59918 to your computer and use it in GitHub Desktop.
JPG照片修复
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
# !/usr/bin/env python | |
__author__ = 'fengxing' | |
__date__ = '2012-1-18 20:13' | |
import sys | |
def jpgfix(name): | |
""" See the doucment: http://en.wikipedia.org/wiki/Jpeg#Syntax_and_structure""" | |
sig = ('\xFF\xD8\xFF\xDB', '\xFF\xD9\xFF\xDB') | |
with open(name, "r") as fd: | |
fd.seek(len(sig), 0) | |
jpg = fd.read() | |
pos = max([jpg.find(s) for s in sig]) | |
if pos < 0: | |
raise Exception('Not find signature') | |
jpg = jpg[pos+4:] | |
with open(name, "w") as fd: | |
fd.seek(0, 0) | |
print 'size is:', len(jpg) | |
fd.write(sig[0]) | |
fd.write(jpg) | |
if __name__ == '__main__': | |
try: | |
while True: | |
jpgfix(sys.argv[1]) | |
except: | |
print 'Done' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment