Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, os
- from PIL import Image
- WARNING = """USAGE: python {this} <infile> [True]
- True by default, set third arg to "False" to disable compression.
- Can result in larger file""".format(this=os.path.split(sys.argv[0])[1])
- if len(sys.argv) not in (2, 3):
- print WARNING
- print sys.argv
- sys.exit(0)
- filename = sys.argv[1]
- compress = True if len(sys.argv) == 2 else eval(sys.argv[2])
- divs = {
- "magic": "PZIMG",
- "mhead": "PZ",
- "res" : "+",
- 'hspl' : "\x01",
- "dat" : "" #Defined by size in bytes
- }
- form = lambda a: divs["dat"].join([''.join([chr(o) for o in i]) for i in a])
- openedFile = Image.open(filename)
- imageSize = openedFile.size
- pixelData = openedFile.getdata()
- def finalize():
- outfile = ""
- outfile += divs["magic"]
- outfile += divs["hspl"]
- datares = [str(i) for i in [imageSize[0], imageSize[1]]]
- outfile += divs["res"].join(datares)
- outfile += divs["hspl"]
- outfile += str(len(pixelData))
- outfile += divs["hspl"]
- outfile += divs["mhead"]+"STARTF{"
- outfile += form(pixelData).encode("zlib") if compress else form(pixelData)
- outfile += "}"
- outfile += divs["mhead"] + "ENDF"
- return outfile
- def main():
- with open(filename+".pzi", "wb") as pz:
- pz.write(finalize())
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement