summaryrefslogtreecommitdiff
path: root/code_refs/image_2_bytes.py
diff options
context:
space:
mode:
Diffstat (limited to 'code_refs/image_2_bytes.py')
-rw-r--r--code_refs/image_2_bytes.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/code_refs/image_2_bytes.py b/code_refs/image_2_bytes.py
new file mode 100644
index 0000000..62524a3
--- /dev/null
+++ b/code_refs/image_2_bytes.py
@@ -0,0 +1,94 @@
+from PIL import Image
+import sys
+import os
+from math import pow, ceil
+
+IMG_HEADER = "uint8_t image[1360] = {"
+IMG_END = "};"
+#
+# ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,!@$%^&(){}[]'"?<>:;\/-+=~`_*|
+#
+byte_array = []
+
+if len(sys.argv) != 3:
+ print("Incorrect arguments supplied. Usage: python image_2_bytes.py <inputfile> <font/img>.")
+ quit()
+
+filename = sys.argv[1]
+
+if not os.path.exists(filename):
+ print("Image file does not exist.")
+ quit()
+
+img = Image.open(filename)
+
+if sys.argv[2] == "img":
+ if img.size != (160, 68):
+ print("Image is of incorrect size, must be 160x68.")
+ quit()
+
+ byte_index = 0
+ current_byte = 0
+ for pixel in list(img.getdata()):
+ if pixel != 0: # if not black, then white
+ current_byte += int(pow(2, (byte_index)))
+ byte_index += 1
+ if byte_index == 8:
+ byte_array.append(str(current_byte))
+ current_byte = 0
+ byte_index = 0
+
+ file_string = IMG_HEADER + ",".join(byte_array) + IMG_END
+ with open("image.txt", "w") as f:
+ f.write(file_string)
+
+ print("Image converted successfully.")
+elif sys.argv[2] == "font":
+ character_width = int(input("Width of each character: "))
+ character_height = int(input("Height of each character: "))
+ if img.size[0] % character_width != 0:
+ print("Image width must be a multiple of the character width.")
+ quit()
+ if img.size[1] % character_height != 0:
+ print("Image height must be a multiple of the character height.")
+ quit()
+
+ bytes_per_char = character_height * ceil(character_width/8)
+
+ charstr = input("Input string of characters present in image: ")
+
+ font_index = [-1 for _ in range(128)]
+ spacechar = "-1"
+ for i, character in enumerate(charstr):
+ if character == " ":
+ spacechar = i
+ font_index[ord(character)] = str(i) # ascii -> glyph index
+ x_offset = i*character_width % img.size[0]
+ y_offset = character_height * (i*character_width // img.size[0])
+ for y in range(character_height):
+ byte_index = 0
+ current_byte = 0
+ for x in range(character_width):
+ if img.getpixel((x_offset+x, y_offset+y)) != 0:
+ current_byte += int(pow(2, (byte_index)))
+ byte_index += 1
+ if byte_index == 8:
+ byte_array.append(str(current_byte))
+ current_byte = 0
+ byte_index = 0
+ if byte_index != 0: # character width isn't multiple of 8
+ byte_array.append(str(current_byte))
+ if spacechar != "-1":
+ for i in range(128):
+ if font_index[i] == -1:
+ font_index[i] = str(spacechar)
+ file_string = f"uint8_t char_w = {character_width};\nuint8_t char_h = {character_height};\nuint16_t bytes_per_char = {bytes_per_char};\n"
+ file_string += f"uint8_t font_bytes[{len(byte_array)}] = " + "{" + ",".join(byte_array) + "};\n"
+ file_string += "int8_t font_lookup[128] = {" + ",".join(font_index) + "};"
+ with open("font.h", "w") as f:
+ f.write(file_string)
+
+ print("Image converted successfully.")
+else:
+ print("Invalid operation. Must be either 'img' or 'font'.")
+ quit() \ No newline at end of file