convert_graphics.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.subcommand('Converts an input image to something QMK understands')
  17. def painter_convert_graphics(cli):
  18. """Converts an image file to a format that Quantum Painter understands.
  19. 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`.
  20. """
  21. # Work out the input file
  22. if cli.args.input != '-':
  23. cli.args.input = normpath(cli.args.input)
  24. # Error checking
  25. if not cli.args.input.exists():
  26. cli.log.error('Input image file does not exist!')
  27. cli.print_usage()
  28. return False
  29. # Work out the output directory
  30. if len(cli.args.output) == 0:
  31. cli.args.output = cli.args.input.parent
  32. cli.args.output = normpath(cli.args.output)
  33. # Ensure we have a valid format
  34. if cli.args.format not in valid_formats.keys():
  35. cli.log.error('Output format %s is invalid. Allowed values: %s' % (cli.args.format, ', '.join(valid_formats.keys())))
  36. cli.print_usage()
  37. return False
  38. # Work out the encoding parameters
  39. format = valid_formats[cli.args.format]
  40. # Load the input image
  41. input_img = Image.open(cli.args.input)
  42. # Convert the image to QGF using PIL
  43. out_data = BytesIO()
  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)
  45. out_bytes = out_data.getvalue()
  46. # Work out the text substitutions for rendering the output data
  47. subs = {
  48. 'generated_type': 'image',
  49. 'var_prefix': 'gfx',
  50. 'generator_command': f'qmk painter-convert-graphics -i {cli.args.input.name} -f {cli.args.format}',
  51. 'year': datetime.date.today().strftime("%Y"),
  52. 'input_file': cli.args.input.name,
  53. 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem),
  54. 'byte_count': len(out_bytes),
  55. 'bytes_lines': render_bytes(out_bytes),
  56. 'format': cli.args.format,
  57. }
  58. # Render the license
  59. subs.update({'license': render_license(subs)})
  60. # Render and write the header file
  61. header_text = render_header(subs)
  62. header_file = cli.args.output / (cli.args.input.stem + ".qgf.h")
  63. with open(header_file, 'w') as header:
  64. print(f"Writing {header_file}...")
  65. header.write(header_text)
  66. header.close()
  67. # Render and write the source file
  68. source_text = render_source(subs)
  69. source_file = cli.args.output / (cli.args.input.stem + ".qgf.c")
  70. with open(source_file, 'w') as source:
  71. print(f"Writing {source_file}...")
  72. source.write(source_text)
  73. source.close()