Created
April 10, 2021 18:55
-
-
Save tobchen/8338bde653244104a750355745bcda28 to your computer and use it in GitHub Desktop.
Convert images to XBM; e.g. "python3 convert.py img.png icon.h" or "python3 convert.py img.png icon.h icon_name"
This file contains hidden or 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
import sys | |
import os | |
from PIL import Image | |
if __name__ == "__main__": | |
if len(sys.argv) < 3: | |
print("Not enough arguments; needs input and output!") | |
else: | |
image = Image.open(sys.argv[1]) | |
image = image.convert("L") | |
width, height = image.size | |
characters = list() | |
data = image.getdata() | |
for i in range(len(data)): | |
if i % 8 == 0: | |
characters.append(0x00) | |
if data[i] < 128: | |
characters[-1] |= 0x01 << (i % 8) | |
name = os.path.splitext(os.path.basename(sys.argv[2]))[0] | |
if len(sys.argv) > 3: | |
name = sys.argv[3] | |
with open(sys.argv[2], "w") as file: | |
file.write("#define {}_width {}\n".format(name, width)) | |
file.write("#define {}_height {}\n".format(name, height)) | |
file.write("static char {}_bits[] = {{{}}};\n".format(name, ", ".join(["0x{:02x}".format(x) for x in characters]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment