import sys
import os
from os import listdir
from os.path import isfile, join
import struct
import shutil


root = "/Users/njoubert/Library/Application Support/MobileSync/Backup/9575474619196fad794f3614fc3d28fe3cff62be"

def main():
    onlyfiles = [ f for f in listdir(root) if isfile(join(root,f)) ]
	for f in onlyfiles:
		FP = open(join(root,f), "rb")
		try:
			byte = struct.unpack('cccc', FP.read(4))
			if (byte == ("\xff","\xd8","\xff","\xe1")):
				print "Copying jpeg", f
				shutil.copy(join(root,f), join(root,f+".jpg"))
		except:
			print "Couldnt read file"
		finally:
			FP.close()
			


if __name__ == '__main__':
	main()