gnucash-docs maint: Modify adjust-dpi.sh to work with Ubuntu

Geert Janssens gjanssens at code.gnucash.org
Sat Sep 3 04:50:43 EDT 2016


Updated	 via  https://github.com/Gnucash/gnucash-docs/commit/e78b8eed (commit)
	from  https://github.com/Gnucash/gnucash-docs/commit/58536bb2 (commit)



commit e78b8eed56378730cce0a4d75d534644c8df6b7c
Author: goodvibes2 <chris.good at ozemail.com.au>
Date:   Mon Aug 29 17:02:54 2016 +1000

    Modify adjust-dpi.sh to work with Ubuntu
    
    Ubuntu 16.04 identify utility in imagemagick Version: 8:6.8.9.9-7ubuntu5.1
    returns an unrounded dpi and does not suffix it with (space)PixelsPerCentimeter

diff --git a/util/adjust-dpi.sh b/util/adjust-dpi.sh
index 37f8033..0717ad7 100755
--- a/util/adjust-dpi.sh
+++ b/util/adjust-dpi.sh
@@ -2,56 +2,40 @@
 # make a list file of all the png figures
 ls *.png > list
 
-for figure in `cat list`;
+for figure in $(cat list);
 do
-# read width in pixel for the figure
-width=$(identify -format "%w" "$figure")
-# if the width is less than 90x14cm/2,54
-if [ "$width" -lt 496 ]; then
-  dpi=90
+  # get width in pixels of the figure
+  width=$(identify -format "%w" "$figure")
+  if [ "$width" -lt 496 ]; then
+    # width is less than 90x14cm/2,54
+    dpi=90
+  else
+    if [ "$width" -gt 793 ]; then
+      # width is more than 144x14cm/2,54
+      # set the new dpi to function of the image size
+      # use awk to round to 0 decimals (note awk, bc + identify ignore
+      #  locale decimal separator and use ".")
+      dpi=$(echo "scale=8; $width*2.54/14" | bc | awk '{ printf("%.0f",$1); }' )
+    else
+      # width between 496px and 793px
+      dpi=144
+    fi
+  fi
   # convert dpi from pixelsperinch to pixelspercentimeter
-  dpi_cm=$(echo "scale=2; $dpi/2.54" | bc)
-  # read the existing dpi from figure as XX PixelsPerCentimeter
+  #  Note: bc truncates to scale decimals so use awk to round to 2 decimals
+  dpi_cm=$(echo "scale=8; $dpi/2.54" | bc | awk '{ printf("%.2f",$1); }')
+  # get the existing dpi from figure as XX PixelsPerCentimeter
   existing_dpi=$(identify -format "%x" "$figure")
-  # set the future dpi of figure as XX PixelsPerCentimeter
-  future_dpi="$dpi_cm PixelsPerCentimeter"
+  # some vers of identify suffix the returned dpi with " PixelsPerCentimeter"
+  #  and/or do not round the return value of identify -format "%x"
+  existing_dpi=$( echo "$existing_dpi" | awk '{ printf("%.2f",$1); }')
+  # set the future dpi figure to XX.XX (PixelsPerCentimeter)
+  future_dpi="$dpi_cm"
   # apply new dpi only if it's changed from the existing
   if [ "$existing_dpi" != "$future_dpi" ]; then
     convert -units PixelsPerInch -density "$dpi" "$figure" "$figure"
-    echo "File $figure converted to $dpi dpi"
-  fi
-# if the width is more than 144x14cm/2,54
-else
-  if [ "$width" -gt 793 ]; then
-    # set the new dpi in function of the image size
-    dpi=$(echo "scale=0; $width*2.54/14" | bc)
-    # convert dpi from pixelsperinch to pixelspercentimeter
-    dpi_cm=$(echo "scale=2; $dpi/2.54" | bc)
-    # read the existing dpi from figure as XX PixelsPerCentimeter
-    existing_dpi=$(identify -format "%x" "$figure")
-    # set the future dpi of figure as XX PixelsPerCentimeter
-    future_dpi="$dpi_cm PixelsPerCentimeter"
-    # apply new dpi only if it's changed from the existing
-    if [ "$existing_dpi" != "$future_dpi" ]; then
-      convert -units PixelsPerInch -density "$dpi" "$figure" "$figure"
-      echo "File $figure converted to $dpi dpi"
-    fi
-# for figures with width between 496px and 793px use a dpi of 144
-  else
-    dpi=144
-    # convert dpi from pixelsperinch to pixelspercentimeter
-    dpi_cm=$(echo "scale=2; $dpi/2.54" | bc)
-    # read the existing dpi from figure as XX PixelsPerCentimeter
-    existing_dpi=$(identify -format "%x" "$figure")
-    # set the future dpi of figure as XX PixelsPerCentimeter
-    future_dpi="$dpi_cm PixelsPerCentimeter"
-    # apply new dpi only if it's changed from the existing
-    if [ "$existing_dpi" != "$future_dpi" ]; then
-      convert -units PixelsPerInch -density 144 "$figure" "$figure"
-      echo "File $figure converted to 144 dpi"
-    fi
+    echo "File $figure converted from $existing_dpi to $dpi dpi"
   fi
-fi
 done
 rm list
 echo "Done!"



Summary of changes:
 util/adjust-dpi.sh | 70 +++++++++++++++++++++---------------------------------
 1 file changed, 27 insertions(+), 43 deletions(-)



More information about the gnucash-changes mailing list