def handleArgv(): global INPUT_PATH global OUTPUT_PREFIX global OUTPUT_PATH INPUT_PATH=sys.argv[1] if os.path.exists(INPUT_PATH): INPUT_PATH=sys.argv[1] else: raise Exception("cannot find input file!")
if len(sys.argv)>=2 and os.path.isdir(sys.argv[2]): OUTPUT_PATH=sys.argv[2] if not OUTPUT_PATH.endswith("/"): OUTPUT_PATH+="/" else: raise Exception("cannot find output folder!")
if "-p" in sys.argv: idx=sys.argv.index("-p") if idx+1< len(sys.argv): OUTPUT_PREFIX=sys.argv[idx+1]
def getInput(): global INPUT_PATH global INPUTS if os.path.isfile(INPUT_PATH): INPUTS=[INPUT_PATH] elif os.path.isdir(INPUT_PATH): if not INPUT_PATH.endswith("/"): INPUT_PATH+="/" INPUTS=[INPUT_PATH+s for s in os.listdir(INPUT_PATH)] else: raise Exception("Input is not a file or directory")
def getOutput(): global OUTPUT_PATH for i in range(1,1+len(INPUTS)): OUTPUTS.append(OUTPUT_PATH+OUTPUT_PREFIX+str(i)+".gif")
def decode(): assert len(INPUTS)==len(OUTPUTS) for input,output in zip(INPUTS,OUTPUTS): with open(input,"rb") as fin: with open(output,"wb") as fout: header=[] for i in range(0,5): header.append(fin.read(2)) unpackHeader=[unpack(">H",i)[0]for i in header] for i in range(0,5): if(unpackHeader[i]&1==1): unpackHeader[i]-=1 else: unpackHeader[i]+=1 for i in unpackHeader: fout.write(pack(">H",i)) fout.write(fin.read())