Last active
November 29, 2021 23:32
-
-
Save Epicpkmn11/6b343b8b580be28d750dd6ebf0fb5203 to your computer and use it in GitHub Desktop.
Converts an image to a font bin for DS or PSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Requirements: | |
# pip3 install pillow | |
""" | |
Copyright © 2021 Pk11 | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the “Software”), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
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 OR COPYRIGHT HOLDERS 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. | |
""" | |
import argparse | |
from PIL import Image | |
import struct | |
parser = argparse.ArgumentParser(description="Converts an image to a font bin for DS or PSP") | |
parser.add_argument("input", metavar="input.png", type=str, nargs=1, help="image to convert from") | |
parser.add_argument("output", metavar="output.bin", type=str, nargs=1, help="file to output to") | |
parser.add_argument("-p", "--psp", action="store_true", help="make for PSP") | |
args = parser.parse_args() | |
with Image.open(args.input[0]) as img: | |
count = img.size[0] * img.size[1] // 8 // 8 | |
px = img.load() | |
with open(args.output[0], "wb") as file: | |
for c in range(count): | |
for i in range(8): | |
# 2bpp | |
if not args.psp: | |
# 4bpp | |
# for j in [0, 2, 4, 6]: | |
# y = (c // 16) * 8 + i | |
# x = (c % 16) * 8 + j | |
# p = px[x, y] | (px[x + 1, y] << 4) | |
# file.write(struct.pack("B", p)) | |
# 2bpp | |
# for j in [0, 4]: | |
# y = (c // 16) * 8 + i | |
# x = (c % 16) * 8 + j | |
# p = 0 | |
# for k in range(4): | |
# p = p | (px[x + k, y] << k * 2) | |
# file.write(struct.pack("B", p)) | |
# 1bpp | |
y = (c // 16) * 8 + i | |
x = (c % 16) * 8 | |
p = 0 | |
for j in range(8): | |
p = p | ((px[x + j, y] & 1) << j) | |
file.write(struct.pack("B", p)) | |
# 1bpp | |
else: | |
y = (c // 16) * 8 + i | |
x = (c % 16) * 8 | |
p = 0 | |
for j in range(8): | |
p = p | (px[x + j, y] << 7 - j) | |
file.write(struct.pack("B", p)) | |
print(f"{args.output[0]} created with {count} tiles.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment