mirror of
https://gist.github.com/404217398b4f35263b5af37c9f9d5800.git
synced 2025-06-18 16:35:33 -04:00
98 lines
3.4 KiB
Python
Executable File
98 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# Requirements:
|
|
# pip3 install pillow
|
|
|
|
"""
|
|
This is free and unencumbered software released into the public domain.
|
|
|
|
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
distribute this software, either in source code form or as a compiled
|
|
binary, for any purpose, commercial or non-commercial, and by any
|
|
means.
|
|
|
|
In jurisdictions that recognize copyright laws, the author or authors
|
|
of this software dedicate any and all copyright interest in the
|
|
software to the public domain. We make this dedication for the benefit
|
|
of the public at large and to the detriment of our heirs and
|
|
successors. We intend this dedication to be an overt act of
|
|
relinquishment in perpetuity of all present and future rights to this
|
|
software under copyright law.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
For more information, please refer to <http://unlicense.org/>
|
|
"""
|
|
|
|
import argparse
|
|
import struct
|
|
|
|
from os import SEEK_CUR
|
|
from os.path import join
|
|
from PIL import Image
|
|
from sys import exit
|
|
|
|
|
|
def pkg2png(args):
|
|
for input in args.inputs:
|
|
print(input.name)
|
|
|
|
type, fileCount = struct.unpack("<LL", input.read(8))
|
|
|
|
if type == 1:
|
|
print("Error! This is an audio pkg")
|
|
exit()
|
|
elif type != 0:
|
|
print(f"Error! This is not a graphics pkg (type {type})")
|
|
exit()
|
|
|
|
# File names
|
|
files = []
|
|
for _ in range(fileCount):
|
|
strLen = struct.unpack("B", input.read(1))[0]
|
|
files.append(input.read(strLen).decode("utf-8"))
|
|
|
|
# File sizes
|
|
remainingSize, fileCount, something = struct.unpack("<LHH", input.read(8))
|
|
sizes = []
|
|
for _ in range(fileCount):
|
|
_, height, width = struct.unpack("<BHH", input.read(5))
|
|
sizes.append((width, height))
|
|
|
|
if something != 0:
|
|
print(f"Header thing: {something} (do -t{something} on re-conversion)")
|
|
|
|
# Image data
|
|
for i, output in enumerate(files):
|
|
size = sizes[i]
|
|
print(output)
|
|
|
|
# Why????
|
|
something = struct.unpack("<H", input.read(2))[0]
|
|
if(something != 1):
|
|
print(f"Weird 0x{something:X} (do -w on re-conversion)")
|
|
input.seek(-1, SEEK_CUR)
|
|
|
|
data = b""
|
|
for _ in range(size[0] * size[1]):
|
|
color = struct.unpack("<H", input.read(2))[0]
|
|
r, g, b = [round(((color >> (i * 5)) & 0x1F) * 255 / 31) for i in range(3)]
|
|
data += struct.pack("BBBB", r, g, b, 0xFF if color & 0x8000 else 0x00)
|
|
|
|
img = Image.frombytes("RGBA", size, data)
|
|
|
|
img.save((join(args.output, output) if args.output else output) + ".png")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pkg2pngarg = argparse.ArgumentParser(description="Converts a PKG file to images")
|
|
pkg2pngarg.add_argument("inputs", metavar="in.pkg", nargs="*", type=argparse.FileType("rb"), help="input file(s)")
|
|
pkg2pngarg.add_argument("--output", "-o", metavar="out", type=str, help="output directory")
|
|
exit(pkg2png(pkg2pngarg.parse_args()))
|