image_to_tft.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. #
  3. # Marlin 3D Printer Firmware
  4. # Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. #
  6. # Based on Sprinter and grbl.
  7. # Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  8. #
  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.
  13. #
  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.
  18. #
  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/>.
  21. #
  22. # Generate Marlin TFT Images from bitmaps/PNG/JPG
  23. import sys, struct
  24. from PIL import Image
  25. def image2bin(image, output_file, transparency):
  26. w, h = image.size[0], image.size[1]
  27. print(f"Converting image with dimensions {w}x{h}...")
  28. if output_file.endswith(('.c', '.cpp')):
  29. is_cpp = True
  30. row_sp, item_sp = (" ", "") if w >= 480 else (" ", " ")
  31. row_end, data_end = "\n", "};\n"
  32. f = open(output_file, 'wt')
  33. f.write("const uint16_t image[%d] = {\n" % (h * w))
  34. else:
  35. is_cpp = False
  36. row_sp, row_end, data_end = b"", b"", b""
  37. f = open(output_file, 'wb')
  38. tcolor, got_tcolor = 0, False
  39. pixs = image.load()
  40. for y in range(h):
  41. f.write(row_sp)
  42. for x in range(w):
  43. R = pixs[x, y][0] >> 3
  44. G = pixs[x, y][1] >> 2
  45. B = pixs[x, y][2] >> 3
  46. rgb = (R << 11) | (G << 5) | B
  47. if transparency:
  48. if not got_tcolor:
  49. got_tcolor = True
  50. tcolor = rgb # First pixel color is transparent
  51. if rgb == tcolor: rgb = 1 # "color 1" is transparent
  52. if is_cpp:
  53. strHex = item_sp + "0x{0:04X},".format(rgb)
  54. f.write(strHex)
  55. else:
  56. f.write(struct.pack("B", (rgb & 0xFF)))
  57. f.write(struct.pack("B", (rgb >> 8) & 0xFF))
  58. f.write(row_end)
  59. f.write(data_end)
  60. f.close()
  61. if len(sys.argv) <= 2:
  62. print("Utility to export a image in Marlin TFT friendly format.")
  63. print("It will dump a raw bin RGB565 image or create a CPP file with an array of 16 bit image pixels.")
  64. print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin) [--transparency]")
  65. print("Authors: rhapsodyv, thinkyhead")
  66. exit(1)
  67. transparency = len(sys.argv) > 3 and sys.argv[3] == "--transparency"
  68. output_img = sys.argv[2]
  69. img = Image.open(sys.argv[1])
  70. image2bin(img, output_img, transparency)