From 85683471eaf1579de608f45efa7c52e7487104de Mon Sep 17 00:00:00 2001 From: Bjorn Lammers Date: Sun, 20 Oct 2024 20:46:29 +0200 Subject: [PATCH] :sparkles: Adds processing of PNG-only files --- scripts/convert_svg_assets.py | 44 ++++++++++++++++++++++++++--------- svg/5etools-dark.svg | 1 + svg/5etools.svg | 1 + 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 svg/5etools-dark.svg create mode 100644 svg/5etools.svg diff --git a/scripts/convert_svg_assets.py b/scripts/convert_svg_assets.py index cad2a6f91..29f1c56bf 100644 --- a/scripts/convert_svg_assets.py +++ b/scripts/convert_svg_assets.py @@ -26,7 +26,9 @@ def file_size_readable(size_bytes): for unit in ['B', 'KB', 'MB', 'GB']: if size_bytes < 1024: return f"{size_bytes:.2f} {unit}" + #size_bytes /= 1024 size_bytes /= 1024 + return f"{size_bytes:.2f} TB" def hash_file(file_path): """Generate an MD5 hash for a file.""" @@ -83,11 +85,11 @@ def convert_svg_to_png(svg_path, png_path): print(f"Failed to convert {svg_path} to PNG: {e}") failed_files.append(svg_path) -def convert_png_to_webp(png_path, webp_path): - """Convert PNG to WEBP.""" +def convert_image_to_webp(image_path, webp_path): + """Convert an image (PNG or other) to WEBP.""" global converted_webps try: - image = Image.open(png_path) + image = Image.open(image_path).convert("RGBA") if needs_conversion(webp_path): image.save(webp_path, format='WEBP') @@ -97,11 +99,11 @@ def convert_png_to_webp(png_path, webp_path): print(f"WEBP already up-to-date: {webp_path}") except Exception as e: - print(f"Failed to convert {png_path} to WEBP: {e}") - failed_files.append(png_path) + print(f"Failed to convert {image_path} to WEBP: {e}") + failed_files.append(image_path) def clean_up_files(folder, valid_basenames): - """Remove files that no longer have corresponding SVG files.""" + """Remove files that no longer have corresponding SVG or PNG files.""" removed_files = 0 for file_path in folder.glob('*'): if file_path.stem not in valid_basenames: @@ -111,7 +113,7 @@ def clean_up_files(folder, valid_basenames): return removed_files if __name__ == "__main__": - # Track valid basenames (existing SVG files) + # Track valid basenames (from SVG and PNG files) valid_basenames = set() # Process all SVG files @@ -137,10 +139,30 @@ def clean_up_files(folder, valid_basenames): # Convert PNG to WEBP if png_path.exists(): - convert_png_to_webp(png_path, webp_path) - - # Clean up unused files - removed_pngs = clean_up_files(PNG_DIR, valid_basenames) + convert_image_to_webp(png_path, webp_path) + + # Process PNG-only files + for png_file in PNG_DIR.glob("*.png"): + if png_file.stem not in valid_basenames: + # Ensure the filename is in kebab-case + try: + png_path = rename_if_needed(png_file) + except Exception as e: + print(f"Error renaming {png_file}: {e}") + failed_files.append(png_file) + continue + + valid_basenames.add(png_path.stem) + + # Set path for WEBP + webp_path = WEBP_DIR / f"{png_path.stem}.webp" + + # Convert PNG to WEBP + convert_image_to_webp(png_path, webp_path) + + # Clean up unused files in PNG and WEBP directories + # Only remove files that don't have corresponding SVG or PNG files + removed_pngs = clean_up_files(PNG_DIR, valid_basenames.union({p.stem for p in SVG_DIR.glob("*.svg")})) removed_webps = clean_up_files(WEBP_DIR, valid_basenames) # Display summary diff --git a/svg/5etools-dark.svg b/svg/5etools-dark.svg new file mode 100644 index 000000000..f44b12233 --- /dev/null +++ b/svg/5etools-dark.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/svg/5etools.svg b/svg/5etools.svg new file mode 100644 index 000000000..618d5f3e6 --- /dev/null +++ b/svg/5etools.svg @@ -0,0 +1 @@ +