Skip to main content

One more example, plus argument parsing

ID
6541ada
date
2021-02-20 23:31:12+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
be0500f
message
One more example, plus argument parsing
changed files
4 files, 123 additions, 33 deletions

Changed files

README.md (98 → 121)

diff --git a/README.md b/README.md
index 5e1b078..9dfbe4c 100644
--- a/README.md
+++ b/README.md
@@ -4,4 +4,6 @@
 
 <img src="mars.svg">
 
-<img src="plants.svg">
\ No newline at end of file
+<img src="plants.svg">
+
+<img src="happy.svg">
\ No newline at end of file

happy.svg (0 → 4663)

diff --git a/happy.svg b/happy.svg
new file mode 100644
index 0000000..aa45e65
--- /dev/null
+++ b/happy.svg
@@ -0,0 +1,43 @@
+<svg viewBox="0 0 1600 900" style="background-color:#ffc905" xmlns="http://www.w3.org/2000/svg">
+
+        <!--
+            Font Awesome Free 5.15.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free
+            (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)
+        -->
+        
+
+        <g transform="rotate(89 250 250)">
+            <g transform="translate(977 -21) scale(0.2154417339694705 0.2154417339694705)"
+               style="fill: #ffdb5c">
+                <ns0:path xmlns:ns0="http://www.w3.org/2000/svg" d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm80 144c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.1 7.3-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm-160 0c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.2 7.4-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm80 280c-60.6 0-134.5-38.3-143.8-93.3-2-11.9 9.4-21.6 20.7-17.9C155.1 330.5 200 336 248 336s92.9-5.5 123.1-15.2c11.4-3.7 22.6 6.1 20.7 17.9-9.3 55-83.2 93.3-143.8 93.3z" />
+            </g>
+        </g>
+
+        <g transform="rotate(213 250 250)">
+            <g transform="translate(1133 731) scale(0.8157151333419226 0.8157151333419226)"
+               style="fill: #fed231">
+                <ns0:path xmlns:ns0="http://www.w3.org/2000/svg" d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm80 144c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.1 7.3-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm-160 0c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.2 7.4-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm80 280c-60.6 0-134.5-38.3-143.8-93.3-2-11.9 9.4-21.6 20.7-17.9C155.1 330.5 200 336 248 336s92.9-5.5 123.1-15.2c11.4-3.7 22.6 6.1 20.7 17.9-9.3 55-83.2 93.3-143.8 93.3z" />
+            </g>
+        </g>
+
+        <g transform="rotate(291 250 250)">
+            <g transform="translate(1235 44) scale(0.6453261890374813 0.6453261890374813)"
+               style="fill: #ffd43c">
+                <ns0:path xmlns:ns0="http://www.w3.org/2000/svg" d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm80 144c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.1 7.3-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm-160 0c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.2 7.4-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm80 280c-60.6 0-134.5-38.3-143.8-93.3-2-11.9 9.4-21.6 20.7-17.9C155.1 330.5 200 336 248 336s92.9-5.5 123.1-15.2c11.4-3.7 22.6 6.1 20.7 17.9-9.3 55-83.2 93.3-143.8 93.3z" />
+            </g>
+        </g>
+
+        <g transform="rotate(26 250 250)">
+            <g transform="translate(333 -14) scale(0.865216428257763 0.865216428257763)"
+               style="fill: #ffd749">
+                <ns0:path xmlns:ns0="http://www.w3.org/2000/svg" d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm80 144c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.1 7.3-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm-160 0c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.2 7.4-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm80 280c-60.6 0-134.5-38.3-143.8-93.3-2-11.9 9.4-21.6 20.7-17.9C155.1 330.5 200 336 248 336s92.9-5.5 123.1-15.2c11.4-3.7 22.6 6.1 20.7 17.9-9.3 55-83.2 93.3-143.8 93.3z" />
+            </g>
+        </g>
+
+        <g transform="rotate(162 250 250)">
+            <g transform="translate(386 448) scale(0.49723494205223295 0.49723494205223295)"
+               style="fill: #fed337">
+                <ns0:path xmlns:ns0="http://www.w3.org/2000/svg" d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm80 144c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.1 7.3-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm-160 0c23.8 0 52.7 29.3 56 71.4.7 8.6-10.8 11.9-14.9 4.5l-9.5-17c-7.7-13.7-19.2-21.6-31.5-21.6s-23.8 7.9-31.5 21.6l-9.5 17c-4.2 7.4-15.6 4-14.9-4.5 3.1-42.1 32-71.4 55.8-71.4zm80 280c-60.6 0-134.5-38.3-143.8-93.3-2-11.9 9.4-21.6 20.7-17.9C155.1 330.5 200 336 248 336s92.9-5.5 123.1-15.2c11.4-3.7 22.6 6.1 20.7 17.9-9.3 55-83.2 93.3-143.8 93.3z" />
+            </g>
+        </g>
+</svg>
\ No newline at end of file

happybackgrounds.py (5138 → 6968)

diff --git a/happybackgrounds.py b/happybackgrounds.py
index ce559eb..3dc3454 100755
--- a/happybackgrounds.py
+++ b/happybackgrounds.py
@@ -1,8 +1,11 @@
 #!/usr/bin/env python
 
+import argparse
 import colorsys
 import os
 import random
+import sys
+import tempfile
 from urllib.request import urlretrieve
 from xml.etree import ElementTree as ET
 import zipfile
@@ -54,7 +57,7 @@ def get_font_awesome_icon_path(*, icon_name):
 
 
 def create_svg(
-    *, background, icon_name, min_icon_count, max_icon_count, min_scale, max_scale
+    *, background, icon_name, min_icon_count, max_icon_count, min_scale, max_scale, out_path=None
 ):
     """
     Creates an SVG file.  Returns the path to the generated SVG.
@@ -63,6 +66,16 @@ def create_svg(
         (e.g. #ff0000).
     :param icon_name: The name of the Font Awesome icon to use
         (e.g. snowflake)
+    :param min_icon_count: What's the smallest number of icons to add?
+    :param max_icon_count: What's the most icons to add?
+    :param min_scale: How small can the icons get?
+    :param max_scale: How big can the icons get?
+    :param out_path: Where to save the file.
+
+    Note: because of the way icons are added, the min/max icon counts
+    are rough estimates, and you may see fewer icons in the viewbox than
+    you actually asked for.
+
     """
     width = 1600
     height = 900
@@ -119,28 +132,55 @@ def create_svg(
 
         lines.append(
             f"""
-        <g transform="translate({x_start} {y_start}) scale({scale} {scale})"
-           style="fill: {fill_color}">
-            <g transform="rotate({rotation_angle} 0 0)">
+        <g transform="rotate({rotation_angle} 250 250)">
+            <g transform="translate({x_start} {y_start}) scale({scale} {scale})"
+               style="fill: {fill_color}">
                 {icon_path}
             </g>
         </g>"""
         )
 
-    # Add the closing tags, write to the file.
+    # Add the closing tags, write to the file.  We tidy up the XML a bit.
     lines.append("</svg>")
 
-    with open("output.svg", "w") as outfile:
+    if out_path is None:
+        out_path, _ = tempfile.mkstemp()
+
+    with open(out_path, "w") as outfile:
         outfile.write("\n".join(lines))
-    return "output.svg"
 
+    return out_path
 
-if __name__ == "__main__":
-    create_svg(
-        background="#d6fcff",
-        icon_name="snowflake",
-        min_icon_count=5,
-        max_icon_count=10,
-        min_scale=0.2,
-        max_scale=1,
+
+def parse_args():
+    parser = argparse.ArgumentParser(
+        description="Create low-contrast backgrounds from Font Awesome icons.  Prints the path to a generated SVG file."
     )
+    parser.add_argument("--background", help="a six-digit CSS colour (e.g. #ff0000)", required=True)
+    parser.add_argument("--icon_name", help="the Font Awesome icon to use (e.g. snowflake)", required=True)
+    parser.add_argument("--min_icon_count", type=int, default=5, help="(default: 5)")
+    parser.add_argument("--max_icon_count", type=int, default=10, help="(default: 15)")
+    parser.add_argument("--min_scale", type=float, default=0.2, help="(default: 0.2)")
+    parser.add_argument("--max_scale", type=float, default=1, help="(default: 1)")
+
+    parser.add_argument("--out_path", help="Where to save the SVG")
+
+    result = parser.parse_args()
+
+    if result.min_icon_count > result.max_icon_count:
+        sys.exit(f"error: --min_icon_count={result.min_icon_count} should be less than or equal to --max_icon_count={result.max_icon_count}")
+
+    if result.min_scale > result.max_scale:
+        sys.exit(f"error: --min_scale={result.min_scale} should be less than or equal to --max_scale={result.max_scale}")
+
+    return {
+        name: getattr(result, name)
+        for name in dir(result)
+        if not name.startswith("_")
+    }
+
+
+if __name__ == "__main__":
+    kwargs = parse_args()
+    out_path = create_svg(**kwargs)
+    print(out_path)

run.sh (734 → 820)

diff --git a/run.sh b/run.sh
index 1ed12d5..9c619d7 100755
--- a/run.sh
+++ b/run.sh
@@ -3,24 +3,24 @@
 set -o errexit
 set -o nounset
 
-# python3 happybackgrounds.py \
-#     --background="#d6fcff" \
-#     --icon_name="snowflake" \
-#     --out_path="snowflake.svg"
+python3 happybackgrounds.py \
+    --background="#d6fcff" \
+    --icon_name="snowflake" \
+    --out_path="snowflake.svg"
 
-# python3 happybackgrounds.py \
-#     --background="#000314" \
-#     --icon_name="star" \
-#     --out_path="starfield.svg" \
-#     --min_scale=0.2 \
-#     --max_scale=0.6
+python3 happybackgrounds.py \
+    --background="#000314" \
+    --icon_name="star" \
+    --out_path="starfield.svg" \
+    --min_scale=0.2 \
+    --max_scale=0.6
 
-# python3 happybackgrounds.py \
-#     --background="#c24400" \
-#     --icon_name="rocket" \
-#     --out_path="mars.svg" \
-#     --min_scale=0.2 \
-#     --max_scale=0.6
+python3 happybackgrounds.py \
+    --background="#c24400" \
+    --icon_name="rocket" \
+    --out_path="mars.svg" \
+    --min_scale=0.2 \
+    --max_scale=0.6
 
 python3 happybackgrounds.py \
     --background="#a7ff99" \
@@ -29,4 +29,9 @@ python3 happybackgrounds.py \
     --min_icon_count=35 \
     --max_icon_count=40 \
     --min_scale=0.1 \
-    --max_scale=0.25
\ No newline at end of file
+    --max_scale=0.25
+
+python3 happybackgrounds.py \
+    --background="#ffc905" \
+    --icon_name="grin-beam" \
+    --out_path="happy.svg"