diff --git a/scripts/LVGLImage.py b/scripts/LVGLImage.py index d2e5f3a789..c5a404f5cd 100755 --- a/scripts/LVGLImage.py +++ b/scripts/LVGLImage.py @@ -308,12 +308,11 @@ def write_c_array_file( stride: int, cf: ColorFormat, filename: str, + outputname: str, premultiplied: bool, compress: CompressMethod, data: bytes): - varname = path.basename(filename).split('.')[0] - varname = varname.replace("-", "_") - varname = varname.replace(".", "_") + varname = path.basename(filename).split('.')[0].replace("-", "_").replace(".", "_") if outputname is None else outputname flags = "0" if compress is not CompressMethod.NONE: @@ -773,7 +772,8 @@ class LVGLImage: def to_c_array(self, filename: str, - compress: CompressMethod = CompressMethod.NONE): + compress: CompressMethod = CompressMethod.NONE, + outputname: str = None): self._check_ext(filename, ".c") self._check_dir(filename) @@ -781,7 +781,7 @@ class LVGLImage: data = LVGLCompressData(self.cf, compress, self.data).compressed else: data = self.data - write_c_array_file(self.w, self.h, self.stride, self.cf, filename, + write_c_array_file(self.w, self.h, self.stride, self.cf, filename, outputname, self.premultiplied, compress, data) @@ -1231,10 +1231,11 @@ class RAWImage(): self.data = data def to_c_array(self, - filename: str): + filename: str, + outputname: str = None): # Image size is set to zero, to let PNG or JPEG decoder to handle it # Stride is meaningless for RAW image - write_c_array_file(0, 0, 0, self.cf, filename, + write_c_array_file(0, 0, 0, self.cf, filename, outputname, False, CompressMethod.NONE, self.data) def from_file(self, @@ -1282,22 +1283,30 @@ class PNGConverter: self.rgb565_dither = rgb565_dither self.nema_gfx = nema_gfx - def _replace_ext(self, input, ext): + def _replace_ext(self, input, ext, outputname: str = None): if self.keep_folder: name, _ = path.splitext(input) else: name, _ = path.splitext(path.basename(input)) + + # change output name to 'outputname', if specified + if outputname is not None: + name = path.join(path.dirname(name), outputname) + output = name + ext output = path.join(self.output, output) return output - def convert(self): + def convert(self, outputname: str): + if len(self.files) > 1 and outputname is not None: + raise BaseException(f"Cannot specify output name when converting more than one file.") + output = [] for f in self.files: if self.cf in (ColorFormat.RAW, ColorFormat.RAW_ALPHA): # Process RAW image explicitly img = RAWImage().from_file(f, self.cf) - img.to_c_array(self._replace_ext(f, ".c")) + img.to_c_array(self._replace_ext(f, ".c", outputname), outputname=outputname) else: img = LVGLImage().from_png(f, self.cf, background=self.background, rgb565_dither=self.rgb565_dither, nema_gfx=self.nema_gfx) img.adjust_stride(align=self.align) @@ -1309,8 +1318,9 @@ class PNGConverter: img.to_bin(self._replace_ext(f, ".bin"), compress=self.compress) elif self.ofmt == OutputFormat.C_ARRAY: - img.to_c_array(self._replace_ext(f, ".c"), - compress=self.compress) + img.to_c_array(self._replace_ext(f, ".c", outputname), + compress=self.compress, + outputname=outputname) elif self.ofmt == OutputFormat.PNG_FILE: img.to_png(self._replace_ext(f, ".png")) @@ -1363,6 +1373,9 @@ def main(): '--output', default="./output", help="Select the output folder, default to ./output") + parser.add_argument('--name', + default=None, + help="Specify name for output file. Only applies when input is a file, not a directory. (Also used for variable name inside .c file when format is 'C')") parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument( 'input', help="the filename or folder to be recursively converted") @@ -1373,6 +1386,9 @@ def main(): files = [args.input] elif path.isdir(args.input): files = list(Path(args.input).rglob("*.[pP][nN][gG]")) + + if args.name is not None: + raise BaseException(f"invalid input: cannot specify --name when input is a directory") else: raise BaseException(f"invalid input: {args.input}") @@ -1401,7 +1417,7 @@ def main(): keep_folder=False, rgb565_dither=args.rgb565dither, nema_gfx=args.nemagfx) - output = converter.convert() + output = converter.convert(args.name) for f, img in output: logging.info(f"len: {img.data_len} for {path.basename(f)} ")