gist-404217398b4f35263b5af3.../png2pkg.py
2022-04-06 04:00:53 -05:00

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.path import basename
from PIL import Image
from sys import exit
def png2pkg(args):
if args.output:
output = args.output
else:
output = open(args.inputs[0][:args.inputs[0].rfind(".")] + ".pkg", "wb")
print(output.name)
# 4 bytes 00 then file count
output.write(struct.pack("<LL", 0, len(args.inputs)))
# Write file names
for input in args.inputs:
inputName = basename(input[:input.rfind(".")])
output.write(struct.pack("B", len(inputName)) + inputName.encode("utf-8"))
# File sizes
sizes = []
totalSize = 0 # Total size remaining after this point
for input in args.inputs:
with Image.open(input) as img:
sizes.append(img.size)
totalSize += (img.width * img.height * 2) + 2
if args.weird:
totalSize -= 1
totalSize += 4 + (5 * len(args.inputs))
output.write(struct.pack("<LHH", totalSize, len(args.inputs), args.headerthing))
for width, height in sizes:
# Not sure what the 0x80 means
output.write(struct.pack("<BHH", 0x80, height, width))
for i, input in enumerate(args.inputs):
print(basename(input))
# not sure what this is
output.write(struct.pack("B" if i == 0 and args.weird else "<H", 1))
img = Image.open(input)
data = b""
for y in range(img.height):
for x in range(img.width):
r, g, b, a = [round(x * 31 / 255) & 0x1F for x in img.getpixel((x, y))]
data += struct.pack("<H", (1 << 15 if a == 0x1F else 0) | b << 10 | g << 5 | r)
output.write(data)
if __name__ == "__main__":
png2pkgarg = argparse.ArgumentParser(description="Converts images to a PKG")
png2pkgarg.add_argument("inputs", metavar="in.pkg", nargs="*", type=str, help="input file(s)")
png2pkgarg.add_argument("--output", "-o", metavar="out", type=argparse.FileType("wb"), help="output file")
png2pkgarg.add_argument("--headerthing", "-t", default=0, type=int, help="somehing in the header, idk what")
png2pkgarg.add_argument("--weird", "-w", action="store_true", help="if this one is weird... idk why")
exit(png2pkg(png2pkgarg.parse_args()))