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