logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

convert_graphics.py (3478B)


  1. """This script tests QGF functionality.
  2. """
  3. from io import BytesIO
  4. from qmk.path import normpath
  5. from qmk.painter import generate_subs, render_header, render_source, valid_formats
  6. from milc import cli
  7. from PIL import Image
  8. @cli.argument('-v', '--verbose', arg_only=True, action='store_true', help='Turns on verbose output.')
  9. @cli.argument('-i', '--input', required=True, help='Specify input graphic file.')
  10. @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.')
  11. @cli.argument('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}')
  12. @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disables the use of RLE when encoding images.')
  13. @cli.argument('-d', '--no-deltas', arg_only=True, action='store_true', help='Disables the use of delta frames when encoding animations.')
  14. @cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QGF file as raw data instead of c/h combo.')
  15. @cli.subcommand('Converts an input image to something QMK understands')
  16. def painter_convert_graphics(cli):
  17. """Converts an image file to a format that Quantum Painter understands.
  18. 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`.
  19. """
  20. # Work out the input file
  21. if cli.args.input != '-':
  22. cli.args.input = normpath(cli.args.input)
  23. # Error checking
  24. if not cli.args.input.exists():
  25. cli.log.error('Input image file does not exist!')
  26. cli.print_usage()
  27. return False
  28. # Work out the output directory
  29. if len(cli.args.output) == 0:
  30. cli.args.output = cli.args.input.parent
  31. cli.args.output = normpath(cli.args.output)
  32. # Ensure we have a valid format
  33. if cli.args.format not in valid_formats.keys():
  34. cli.log.error('Output format %s is invalid. Allowed values: %s' % (cli.args.format, ', '.join(valid_formats.keys())))
  35. cli.print_usage()
  36. return False
  37. # Work out the encoding parameters
  38. format = valid_formats[cli.args.format]
  39. # Load the input image
  40. input_img = Image.open(cli.args.input)
  41. # Convert the image to QGF using PIL
  42. out_data = BytesIO()
  43. metadata = []
  44. 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, metadata=metadata)
  45. out_bytes = out_data.getvalue()
  46. if cli.args.raw:
  47. raw_file = cli.args.output / f"{cli.args.input.stem}.qgf"
  48. with open(raw_file, 'wb') as raw:
  49. raw.write(out_bytes)
  50. return
  51. # Work out the text substitutions for rendering the output data
  52. subs = generate_subs(cli, out_bytes, image_metadata=metadata, command_name="painter_convert_graphics")
  53. # Render and write the header file
  54. header_text = render_header(subs)
  55. header_file = cli.args.output / f"{cli.args.input.stem}.qgf.h"
  56. with open(header_file, 'w') as header:
  57. print(f"Writing {header_file}...")
  58. header.write(header_text)
  59. # Render and write the source file
  60. source_text = render_source(subs)
  61. source_file = cli.args.output / f"{cli.args.input.stem}.qgf.c"
  62. with open(source_file, 'w') as source:
  63. print(f"Writing {source_file}...")
  64. source.write(source_text)