qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

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 
      9 
     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=f'Output format, valid types: {", ".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 
     21     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`.
     22     """
     23     # Work out the input file
     24     if cli.args.input != '-':
     25         cli.args.input = normpath(cli.args.input)
     26 
     27         # Error checking
     28         if not cli.args.input.exists():
     29             cli.log.error('Input image file does not exist!')
     30             cli.print_usage()
     31             return False
     32 
     33     # Work out the output directory
     34     if len(cli.args.output) == 0:
     35         cli.args.output = cli.args.input.parent
     36     cli.args.output = normpath(cli.args.output)
     37 
     38     # Ensure we have a valid format
     39     if cli.args.format not in valid_formats.keys():
     40         cli.log.error('Output format %s is invalid. Allowed values: %s' % (cli.args.format, ', '.join(valid_formats.keys())))
     41         cli.print_usage()
     42         return False
     43 
     44     # Work out the encoding parameters
     45     format = valid_formats[cli.args.format]
     46 
     47     # Load the input image
     48     input_img = Image.open(cli.args.input)
     49 
     50     # Convert the image to QGF using PIL
     51     out_data = BytesIO()
     52     metadata = []
     53     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)
     54     out_bytes = out_data.getvalue()
     55 
     56     if cli.args.raw:
     57         raw_file = cli.args.output / f"{cli.args.input.stem}.qgf"
     58         with open(raw_file, 'wb') as raw:
     59             raw.write(out_bytes)
     60         return
     61 
     62     # Work out the text substitutions for rendering the output data
     63     subs = generate_subs(cli, out_bytes, image_metadata=metadata, command_name="painter_convert_graphics")
     64 
     65     # Render and write the header file
     66     header_text = render_header(subs)
     67     header_file = cli.args.output / f"{cli.args.input.stem}.qgf.h"
     68     with open(header_file, 'w') as header:
     69         print(f"Writing {header_file}...")
     70         header.write(header_text)
     71 
     72     # Render and write the source file
     73     source_text = render_source(subs)
     74     source_file = cli.args.output / f"{cli.args.input.stem}.qgf.c"
     75     with open(source_file, 'w') as source:
     76         print(f"Writing {source_file}...")
     77         source.write(source_text)