jpg_to_sjpg.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ##################################################################
  2. # sjpeg converter script version 1.0
  3. # Dependencies: (PYTHON-3)
  4. ##################################################################
  5. SJPG_FILE_FORMAT_VERSION = "V1.00" #
  6. JPEG_SPLIT_HEIGHT = 16
  7. ##################################################################
  8. import math, os, sys, time
  9. from PIL import Image
  10. OUTPUT_FILE_NAME = ""
  11. INPUT_FILE = ""
  12. if len(sys.argv) == 2:
  13. INPUT_FILE = sys.argv[1]
  14. OUTPUT_FILE_NAME = INPUT_FILE.split("/")[-1].split("\\")[-1].split(".")[0]
  15. else:
  16. print("usage:\n\t python " + sys.argv[0] + " input_file.jpg")
  17. sys.exit(0)
  18. try:
  19. im = Image.open(INPUT_FILE)
  20. except:
  21. print("\nFile not found!")
  22. sys.exit(0)
  23. print("\nConversion started...\n")
  24. start_time = time.time()
  25. width, height = im.size
  26. print("Input:")
  27. print("\t" + INPUT_FILE)
  28. print("\tRES = " + str(width) + " x " + str(height) + '\n')
  29. lenbuf = []
  30. block_size = JPEG_SPLIT_HEIGHT;
  31. spilts = math.ceil(height/block_size)
  32. c_code = '''//LVGL SJPG C ARRAY\n#include "lvgl/lvgl.h"\n\nconst uint8_t ''' + OUTPUT_FILE_NAME + '''_map[] = {\n'''
  33. sjpeg_data = bytearray()
  34. sjpeg = bytearray()
  35. row_remaining = height;
  36. for i in range(spilts):
  37. if row_remaining < block_size:
  38. crop = im.crop((0, i*block_size, width, row_remaining + i*block_size))
  39. else:
  40. crop = im.crop((0, i*block_size, width, block_size + i*block_size))
  41. row_remaining = row_remaining - block_size;
  42. crop.save(str(i)+".jpg", quality=90)
  43. for i in range(spilts):
  44. f = open(str(i)+".jpg", "rb")
  45. a = f.read()
  46. f.close()
  47. sjpeg_data = sjpeg_data + a
  48. lenbuf.append(len(a))
  49. header = bytearray()
  50. #4 BYTES
  51. header = header + bytearray("_SJPG__".encode("UTF-8"));
  52. #6 BYTES VERSION
  53. header = header + bytearray(("\x00" + SJPG_FILE_FORMAT_VERSION + "\x00").encode("UTF-8"));
  54. #WIDTH 2 BYTES
  55. header = header + width.to_bytes(2, byteorder='little');
  56. #HEIGHT 2 BYTES
  57. header = header + height.to_bytes(2, byteorder='little');
  58. #NUMBER OF ITEMS 2 BYTES
  59. header = header + spilts.to_bytes(2, byteorder='little');
  60. #NUMBER OF ITEMS 2 BYTES
  61. header = header + int(JPEG_SPLIT_HEIGHT).to_bytes(2, byteorder='little');
  62. for item_len in lenbuf:
  63. # WIDTH 2 BYTES
  64. header = header + item_len.to_bytes(2, byteorder='little');
  65. data = bytearray()
  66. sjpeg = header + sjpeg_data;
  67. if 1:
  68. for i in range(len(lenbuf)):
  69. os.remove(str(i) + ".jpg")
  70. f = open(OUTPUT_FILE_NAME+".sjpg","wb");
  71. f.write(sjpeg)
  72. f.close()
  73. new_line_threshold = 0
  74. for i in range(len(sjpeg)):
  75. c_code = c_code + "\t" + str(hex(sjpeg[i])) + ","
  76. new_line_threshold = new_line_threshold + 1
  77. if (new_line_threshold >= 16):
  78. c_code = c_code + "\n"
  79. new_line_threshold = 0
  80. c_code = c_code + "\n};\n\nlv_img_dsc_t "
  81. c_code = c_code + OUTPUT_FILE_NAME + " = {\n"
  82. c_code = c_code + "\t.header.always_zero = 0,\n"
  83. c_code = c_code + "\t.header.w = " + str(width) + ",\n"
  84. c_code = c_code + "\t.header.h = " + str(height) + ",\n"
  85. c_code = c_code + "\t.data_size = " + str(len(sjpeg)) + ",\n"
  86. c_code = c_code + "\t.header.cf = LV_IMG_CF_RAW,\n"
  87. c_code = c_code + "\t.data = " + OUTPUT_FILE_NAME+"_map" + ",\n};"
  88. f = open(OUTPUT_FILE_NAME + '.c', 'w')
  89. f.write(c_code)
  90. f.close()
  91. time_taken = (time.time() - start_time)
  92. print("Output:")
  93. print("\tTime taken = " + str(round(time_taken,2)) + " sec")
  94. print("\tbin size = " + str(round(len(sjpeg)/1024, 1)) + " KB" )
  95. print("\t" + OUTPUT_FILE_NAME + ".sjpg\t(bin file)" + "\n\t" + OUTPUT_FILE_NAME + ".c\t\t(c array)")
  96. print("\nAll good!")