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
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')
32 f
.write("const uint16_t image[%d] = {\n" % (image
.size
[1] * image
.size
[0]))
34 f
= open(output_file
, 'wb')
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
44 strHex
= '0x{0:04X}, '.format(rgb
)
47 f
.write(struct
.pack("B", (rgb
& 0xFF)))
48 f
.write(struct
.pack("B", (rgb
>> 8) & 0xFF))
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")
62 output_img
= sys
.argv
[2]
63 img
= Image
.open(sys
.argv
[1])
64 image2bin(img
, output_img
)