convert_graphics.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """This script tests QGF functionality.
  2. """
  3. import re
  4. import datetime
  5. from io import BytesIO
  6. from qmk.path import normpath
  7. from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats
  8. from milc import cli
  9. from PIL import Image
  10. @cli.argument('-v', '--verbose', arg_only=True, action='store_true', help='Turns on verbose output.')
  11. @cli.argument('-i', '--input', required=True, help='Specify input graphic file.')
  12. @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.')
  13. @cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys())))
  14. @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disables the use of RLE when encoding images.')
  15. @cli.argument('-d', '--no-deltas', arg_only=True, action='store_true', help='Disables the use of delta frames when encoding animations.')
  16. @cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QGF file as raw data instead of c/h combo.')
  17. @cli.subcommand('Converts an input image to something QMK understands')
  18. def painter_convert_graphics(cli):
  19. """Converts an image file to a format that Quantum Painter understands.
  20. This command uses the `qmk.painter` module to generate a Quantum Painter image defintion from an image. The generated definitions are written to a files next to the input -- `INPUT.c` and `INPUT.h`.
  21. """
  22. # Work out the input file
  23. if cli.args.input != '-':
  24. cli.args.input = normpath(cli.args.input)
  25. # Error checking
  26. if not cli.args.input.exists():
  27. cli.log.error('Input image file does not exist!')
  28. cli.print_usage()
  29. return False
  30. # Work out the output directory
  31. if len(cli.args.output) == 0:
  32. cli.args.output = cli.args.input.parent
  33. cli.args.output = normpath(cli.args.output)
  34. # Ensure we have a valid format
  35. if cli.args.format not in valid_formats.keys():
  36. cli.log.error('Output format %s is invalid. Allowed values: %s' % (cli.args.format, ', '.join(valid_formats.keys())))
  37. cli.print_usage()
  38. return False
  39. # Work out the encoding parameters
  40. format = valid_formats[cli.args.format]
  41. # Load the input image
  42. input_img = Image.open(cli.args.input)
  43. # Convert the image to QGF using PIL
  44. out_data = BytesIO()
  45. input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose)
  46. out_bytes = out_data.getvalue()
  47. if cli.args.raw:
  48. raw_file = cli.args.output / (cli.args.input.stem + ".qgf")
  49. with open(raw_file, 'wb') as raw:
  50. raw.write(out_bytes)
  51. return
  52. # Work out the text substitutions for rendering the output data
  53. subs = {
  54. 'generated_type': 'image',
  55. 'var_prefix': 'gfx',
  56. 'generator_command': f'qmk painter-convert-graphics -i {cli.args.input.name} -f {cli.args.format}',
  57. 'year': datetime.date.today().strftime("%Y"),
  58. 'input_file': cli.args.input.name,
  59. 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem),
  60. 'byte_count': len(out_bytes),
  61. 'bytes_lines': render_bytes(out_bytes),
  62. 'format': cli.args.format,
  63. }
  64. # Render the license
  65. subs.update({'license': render_license(subs)})
  66. # Render and write the header file
  67. header_text = render_header(subs)
  68. header_file = cli.args.output / (cli.args.input.stem + ".qgf.h")
  69. with open(header_file, 'w') as header:
  70. print(f"Writing {header_file}...")
  71. header.write(header_text)
  72. header.close()
  73. # Render and write the source file
  74. source_text = render_source(subs)
  75. source_file = cli.args.output / (cli.args.input.stem + ".qgf.c")
  76. with open(source_file, 'w') as source:
  77. print(f"Writing {source_file}...")
  78. source.write(source_text)
  79. source.close()