-
-
Notifications
You must be signed in to change notification settings - Fork 14
Enhance Icon Generation: Optimize PNGs, Add WebP and SVG Outputs #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
1. Optimizes PNG generation: after writing each PNG, the script runs OptiPNG -o7 to losslessly compress the file. Reduces average PNG size by roughly 50% 2. Adds WebP output: uses Pillow to open the newly generated PNG and save a WebP version at quality=85. Produces a smaller, broadly supported raster format alongside the existing PNG 3. Adds SVG output: adds a new function (create_svg_context()) to create a Cairo SVGSurface. Re-draws each icon onto the SVG canvas and calls show_page(). Provides a resolution-independent vector version of every icon. 4. Impact: Smaller PNGs reduce bandwidth, while adding WebP further cuts file size for modern browsers. SVG provides infinitely scalable format that never blurs, which is essential for high DPI displays and print materials 5. Dependencies: To build and test locally, you will need: a. OptiPNG installed in your path b. Pillow Thank you very much for reviewing, this is my first pull request! Please let me know if any adjustments are needed.
@SamiS0830 thank you for this pull request (PR)! I haven't touched this code base in quite a while, so I expect it will take me a week or two to review this PR. |
This comment was marked as duplicate.
This comment was marked as duplicate.
Thank you very much, I really appreciate it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great work. With an optimization level of 2 and the WebP and SVG files removed this results in:
- ~16% reduction in total size (
127M➡️ 107M) - only a modest increase in script duration (
14 sec➡️ 2 min 59 sec)
@@ -195,6 +202,22 @@ def main(): | |||
# Will raise and exception on error | |||
ctx.get_target().write_to_png(filepath) | |||
|
|||
# Optimize the just‐saved PNG with optipng (lossless compression) | |||
os.system(f"optipng -o7 {filepath}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please quiet the output and set optimization level to 2:
os.system(f"optipng -o2 -quiet {filepath}")
File size comparison in bytes:
File prefix | Prev | o1 | o2 | o3 | o5 | o7 |
---|---|---|---|---|---|---|
www/i/l/by-sa/000000/ff/ff/ff/76x22 |
1105 | 582 | 553 | 553 | 553 | 553 |
www/i/l/by-sa/000000/ff/ff/ff/88x31 |
17096 | 871 | 829 | 829 | 829 | 824 |
Script duration in seconds
Prev | o1 | o2 | o3 | o5 | o7 |
---|---|---|---|---|---|
14 | 132 | 179 | 240 | 434 | 1130 |
@@ -135,6 +136,12 @@ def genicon( | |||
show_chars(ctx, characters, foreground, padding, width, height) | |||
return ctx | |||
|
|||
#function to create a Cairo canvas that write to a SVG file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a space after the hash and capitalize "function"
# Convert PNG -> WebP (smaller file size, quality=85) | ||
img = Image.open(filepath) | ||
webp_filepath = filepath.replace(".png", ".webp") | ||
img.save(webp_filepath, format="webp", quality=85) | ||
|
||
# Also render the same icon as SVG | ||
svg_filepath = filepath.replace(".png", ".svg") | ||
svg_ctx = create_svg_context(width, height, svg_filepath) | ||
set_background(svg_ctx, background, width, height) | ||
configure_font(svg_ctx, font_size) | ||
show_chars(svg_ctx, chars, foreground, padding, width, height) | ||
svg_ctx.show_page() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove WebP and SVG output. Both result in larger files than PNG.
File type size comparison in bytes:
File prefix | PNG | SVG | scour | WebP |
---|---|---|---|---|
www/i/l/by-sa/000000/ff/ff/ff/76x22 |
553 | 4471 | 2854 | 588 |
www/i/l/by-sa/000000/ff/ff/ff/88x31 |
829 | 4531 | 2813 | 858 |
The "scour" column represents SVGs updated using scour-project/scour: Scour - An SVG Optimizer / Cleaner
python3-gi-cairo \ | ||
python3-pil \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove python3-pil
WebP dependency (see comment on lines +208 to +219 for rationale)
@@ -4,6 +4,7 @@ | |||
# Standard library | |||
from functools import reduce | |||
from itertools import product | |||
from PIL import Image |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove WebP related import (see comment on lines +208 to +219 for rationale)
Thank you very much for reviewing, this is my first pull request! Please let me know if any adjustments are needed.
Fixes
Description
Enhances Icon Generation: 1) compresses PNGs 2) Produces a smaller, broadly supported file (WebP) 3) Adds SVG output
optipng -o7
to losslessly compress the file. Reduces average PNG size by roughly 50%.create_svg_context()
) to create a Cairo SVGSurface. Re-draws each icon onto the SVG canvas and calls (show_page()
). Provides a resolution-independent vector version of every icon.a. OptiPNG installed in your path
b. Pillow
Technical details
Requires OptiPNG installed in your path and Pillow
Tests
To verify this PR fixes the problem, run the script and confirm it generates PNG files that are roughly 50% smaller in size, alongside corresponding SVG and WebP files for each PNG. Check that all three formats open correctly without quality loss, and test with different inputs to ensure consistent output and no errors
Checklist
Update index.md
).main
ormaster
).visible errors.
Developer Certificate of Origin
For the purposes of this DCO, "license" is equivalent to "license or public domain dedication," and "open source license" is equivalent to "open content license or public domain dedication."
Developer Certificate of Origin