Skip to content

Instantly share code, notes, and snippets.

@ariankordi
Created July 26, 2017 08:54
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 ariankordi/d42ecdbefa1dc20d60175cae6fb98213 to your computer and use it in GitHub Desktop.
Save ariankordi/d42ecdbefa1dc20d60175cae6fb98213 to your computer and use it in GitHub Desktop.
A little useless: extract a TGA from a Splatoon POST request then convert it to PNG.
import sys, os
from PIL import Image
if len(sys.argv) < 2:
print("Usage: {0} [POST data file]".format(sys.argv[0]))
sys.exit(1)
try:
file = open(sys.argv[1], mode="rb").read()
except Exception as ex:
print("Failed to open {0}.\r\n\r\n{1}".format(sys.argv[1], str(ex)))
sys.exit(1)
# The 'Content-Type: image/tga' text, line feed, everything before the actual file
bf = b"\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x44\x69\x73\x70\x6F\x73\x69\x74\x69\x6F\x6E\x3A\x20\x66\x6F\x72\x6D\x2D\x64\x61\x74\x61\x3B\x20\x6E\x61\x6D\x65\x3D\x22\x46\x61\x63\x65\x49\x6D\x67\x22\x3B\x20\x66\x69\x6C\x65\x6E\x61\x6D\x65\x3D\x22\x66\x61\x63\x65\x49\x6D\x67\x2E\x74\x67\x61\x22\x0D\x0A\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x54\x79\x70\x65\x3A\x20\x69\x6D\x61\x67\x65\x2F\x74\x67\x61\x0D\x0A\x0D\x0A"
# The end of the TGA header
af = b"\x54\x52\x55\x45\x56\x49\x53\x49\x4F\x4E\x2D\x58\x46\x49\x4C\x45\x2E\x00"
i1 = file.index(bf) + 99
writename = os.path.splitext(sys.argv[1])[0] + "_out"
writename_tga = writename + ".tga"
writename_png = writename + ".png"
# Now that we have the tga, let's write it
targa = file[i1:-32]
try:
tga = open(writename_tga, "bw")
tga.write(targa)
except Exception as ex:
print("Failed to convert tga or write to {0}.\r\n\r\n{1}".format(writename_tga, str(ex)))
sys.exit(1)
# Let's also convert the tga to png
im = Image.open(writename_tga)
imbg = im.convert()
imbg.save(writename_png, quality=100)
# And we're finished?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment