Skip to content

Instantly share code, notes, and snippets.

@annidy
Created February 9, 2016 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annidy/8c0efc67d740eee59918 to your computer and use it in GitHub Desktop.
Save annidy/8c0efc67d740eee59918 to your computer and use it in GitHub Desktop.
JPG照片修复
# -*- 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