🧑‍💻 Update Python indentation
[Marlin.git] / buildroot / share / scripts / gen-tft-image.py
blobddbab236ee9113fa310bf0bc136f47a394eb528d
1 #!/usr/bin/env python3
3 # Marlin 3D Printer Firmware
4 # Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
6 # Based on Sprinter and grbl.
7 # Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
23 # Generate Marlin TFT Images from bitmaps/PNG/JPG
25 import sys,re,struct
26 from PIL import Image,ImageDraw
28 def image2bin(image, output_file):
29 if output_file.endswith(('.c', '.cpp')):
30 f = open(output_file, 'wt')
31 is_cpp = True
32 f.write("const uint16_t image[%d] = {\n" % (image.size[1] * image.size[0]))
33 else:
34 f = open(output_file, 'wb')
35 is_cpp = False
36 pixs = image.load()
37 for y in range(image.size[1]):
38 for x in range(image.size[0]):
39 R = pixs[x, y][0] >> 3
40 G = pixs[x, y][1] >> 2
41 B = pixs[x, y][2] >> 3
42 rgb = (R << 11) | (G << 5) | B
43 if is_cpp:
44 strHex = '0x{0:04X}, '.format(rgb)
45 f.write(strHex)
46 else:
47 f.write(struct.pack("B", (rgb & 0xFF)))
48 f.write(struct.pack("B", (rgb >> 8) & 0xFF))
49 if is_cpp:
50 f.write("\n")
51 if is_cpp:
52 f.write("};\n")
53 f.close()
55 if len(sys.argv) <= 2:
56 print("Utility to export a image in Marlin TFT friendly format.")
57 print("It will dump a raw bin RGB565 image or create a CPP file with an array of 16 bit image pixels.")
58 print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin)")
59 print("Author: rhapsodyv")
60 exit(1)
62 output_img = sys.argv[2]
63 img = Image.open(sys.argv[1])
64 image2bin(img, output_img)