-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 commentThe 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) |
|||||||||||||||||||||||||||||||||||
| import errno | |||||||||||||||||||||||||||||||||||
| import math | |||||||||||||||||||||||||||||||||||
| import os | |||||||||||||||||||||||||||||||||||
|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Please add a space after the hash and capitalize "function" |
|||||||||||||||||||||||||||||||||||
| def create_svg_context(width, height, filepath): | |||||||||||||||||||||||||||||||||||
| surface = cairo.SVGSurface(filepath, width, height) | |||||||||||||||||||||||||||||||||||
| ctx = cairo.Context(surface) | |||||||||||||||||||||||||||||||||||
| return ctx | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| def main(): | |||||||||||||||||||||||||||||||||||
| font_map = pangocairo.font_map_get_default() | |||||||||||||||||||||||||||||||||||
|
|
@@ -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 commentThe 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:
Script duration in seconds
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| # 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() | |||||||||||||||||||||||||||||||||||
|
Comment on lines
+208
to
+219
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
The "scour" column represents SVGs updated using scour-project/scour: Scour - An SVG Optimizer / Cleaner |
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | |||||||||||||||||||||||||||||||||||
| try: | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this 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
python3-pilWebP dependency (see comment on lines +208 to +219 for rationale)