adjust-dpi.sh rounding problem

Geert Janssens geert.gnucash at kobaltwit.be
Tue Aug 23 12:27:22 EDT 2016


On Monday 15 August 2016 13:11:27 Chris Good wrote:
> Hi Geert,
> 
> 
> 
> There's a couple of problems with gnucash-docs/util/adjust-dpi.sh on
> Ubuntu 16.04 which makes it update (almost?) every image file each
> time it runs.
> 
> 
> 
> 1. The identify utility in imagemagick Version: 8:6.8.9.9-7ubuntu5.1
> returns an unrounded dpi. E.g
> 
> 
> 
> existing_dpi=$(identify -format "%x" "$figure")
> 
> echo $existing_dpi
> 
> 35.429999999999999716
> 
> 
> 
> 2. It seems this script expects $existing_dpi to be suffixed with
> (space)PixelsPerCentimeter but this doesn't happen for me.
> 
> 
> 
> I was thinking of fixing these problems by using something like:
> 
>                 existing_dpi=$(identify -format "%x" "$figure")
> 
>                 existing_dpi=${existing_dpi% PixelsPerCentimeter}     
>   # strip possible trailing (space)PixelsPerCentimeter
> 
>                 existing_dpi=$( printf '%.2f' "$existing_dpi" )
> # round to 2 decimals
> 
> 
> 
> I guess there is a way to do this all on 1 line but I'd have to do
> some research and I prefer KISS principle anyway.
> 
> 
> 
> Is this portable enough?

Hi Chris,

I find that my identify version isn't printing the PixelsPerCentimeter 
suffix either (Fedora 23, ImageMagick-6.9.2.7-1.fc23.x86_64). Your code 
to strip it is good to catch both ways. You'll have to alter the script 
to not add it to the reference resolution either or you'd still be 
comparing apples to oranges...

As for portability, I find that printf is locale-sensitive (it takes a 
',' as decimal separator in my locale) while the output of identify is 
always using a '.' for decimal separator.

So instead of printf, you may want to reuse the "bc" command like so:
existing_dpi=$(echo "scale=2; $existing_dpi/1.00" | bc)

And still this will need some refinement, because bc (as well as printf) 
don't *round*, they *truncate*. The result of the calculation in this 
case would be 35.42, while the reference resolution is 35.43. I'd have 
to research more to find a routine that properly rounds instead of 
truncates, but I'm out of time right now...

Regards,

Geert


More information about the gnucash-devel mailing list