[Bf-docboard-svn] bf-manual: [9099] trunk/blender_docs/tools_maintenance/image_optimize.py: Image Optimize Improvements

Aaron Carlisle noreply at blender.org
Fri Mar 11 08:11:37 CET 2022


Revision: 9099
          https://developer.blender.org/rBM9099
Author:   Blendify
Date:     2022-03-11 08:11:37 +0100 (Fri, 11 Mar 2022)
Log Message:
-----------
Image Optimize Improvements

- Use multiprocessing
- Remove debug print
- Only loop through files in manual/images before it would try to compress images in the build directory

Modified Paths:
--------------
    trunk/blender_docs/tools_maintenance/image_optimize.py

Modified: trunk/blender_docs/tools_maintenance/image_optimize.py
===================================================================
--- trunk/blender_docs/tools_maintenance/image_optimize.py	2022-03-11 07:05:20 UTC (rev 9098)
+++ trunk/blender_docs/tools_maintenance/image_optimize.py	2022-03-11 07:11:37 UTC (rev 9099)
@@ -6,7 +6,9 @@
 Currently only performs lossless compression on PNG images using 'pngcrush'.
 """
 
+import multiprocessing
 import os
+
 BASEDIR = os.path.abspath(os.path.dirname(__file__))
 
 VERBOSE = False
@@ -46,48 +48,54 @@
                 yield os.path.join(dirpath, filename)
 
 
-def main():
-    vcs_dir = find_vcs_root(BASEDIR)
-    print(vcs_dir)
-    if vcs_dir is None:
-        print("Can't find version directory, aborting!")
-        return
+def exec(f):
+    # ensure we never used a stale file
+    TMP_NAME = f + ".tmp"
 
-    TMP_NAME = "out.png"
-    for f in sorted(files_recursive(vcs_dir, ".png")):
+    if os.path.exists(TMP_NAME):
+        os.remove(TMP_NAME)
 
-        # ensure we never used a stale file
-        if os.path.exists(TMP_NAME):
-            os.remove(TMP_NAME)
+    if os.path.exists(TMP_NAME):
+        print("Error removing %r" % TMP_NAME)
 
-        if os.path.exists(TMP_NAME):
-            print("Error removing %r" % TMP_NAME)
+    cmd = [
+        "pngcrush",
+        "-brute",
+        "-reduce",
+        "-m", "7",
+        f,
+        TMP_NAME,
+    ]
 
-        cmd = [
-            "pngcrush",
-            "-brute",
-            "-reduce",
-            "-m", "7",
-            f,
-            TMP_NAME,
-        ]
+    run(cmd)
 
-        run(cmd)
+    if os.path.exists(TMP_NAME):
+        s = os.path.getsize(f)
+        d = os.path.getsize(TMP_NAME)
 
-        if os.path.exists(TMP_NAME):
-            s = os.path.getsize(f)
-            d = os.path.getsize(TMP_NAME)
+        if d != 0:
+            fac = d / s
+            if fac < (1.0 - FACTOR):
+                print("Replacing: %r" % f)
+                os.remove(f)
+                os.rename(TMP_NAME, f)
+            else:
+                os.remove(TMP_NAME)
 
-            if d != 0:
-                fac = d / s
-                if fac < (1.0 - FACTOR):
-                    print("Replacing: %r" % f)
-                    os.remove(f)
-                    os.rename(TMP_NAME, f)
 
-    if os.path.exists(TMP_NAME):
-        os.remove(TMP_NAME)
+def main():
+    vcs_dir = find_vcs_root(BASEDIR)
+    if vcs_dir is None:
+        print("Can't find version directory, aborting!")
+        return
 
+    images = os.path.join(vcs_dir, "manual", "images")
 
+    pool = multiprocessing.Pool()
+    pool.map_async(exec, files_recursive(images, ".png"))
+    pool.close()
+    pool.join()
+
+
 if __name__ == "__main__":
     main()



More information about the Bf-docboard-svn mailing list