Advertisement
Peaser

Second attempt

Feb 26th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import sys, os
  2. from PIL import Image
  3.  
  4. WARNING = """USAGE: python {this} <infile> [True]
  5. True by default, set third arg to "False" to disable compression.
  6. Can result in larger file""".format(this=os.path.split(sys.argv[0])[1])
  7.  
  8. if len(sys.argv) not in (2, 3):
  9.     print WARNING
  10.     print sys.argv
  11.     sys.exit(0)
  12.  
  13. filename = sys.argv[1]
  14. compress = True if len(sys.argv) == 2 else eval(sys.argv[2])
  15.  
  16. divs = {
  17.     "magic": "PZIMG",
  18.     "mhead": "PZ",
  19.     "res"  : "+",
  20.     'hspl' : "\x01",
  21.     "dat"  : "" #Defined by size in bytes
  22. }
  23.  
  24. form = lambda a: divs["dat"].join([''.join([chr(o) for o in i]) for i in a])
  25. openedFile = Image.open(filename)
  26. imageSize  = openedFile.size
  27. pixelData  = openedFile.getdata()
  28.  
  29. def finalize():
  30.     outfile = ""
  31.     outfile += divs["magic"]
  32.     outfile += divs["hspl"]
  33.     datares = [str(i) for i in [imageSize[0], imageSize[1]]]
  34.     outfile += divs["res"].join(datares)
  35.     outfile += divs["hspl"]
  36.     outfile += str(len(pixelData))
  37.     outfile += divs["hspl"]
  38.     outfile += divs["mhead"]+"STARTF{"
  39.     outfile += form(pixelData).encode("zlib") if compress else form(pixelData)
  40.     outfile += "}"
  41.     outfile += divs["mhead"] + "ENDF"
  42.     return outfile
  43.  
  44. def main():
  45.     with open(filename+".pzi", "wb") as pz:
  46.         pz.write(finalize())
  47.  
  48. if __name__ == "__main__":
  49.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement