Thursday 25 April 2013

Speeding up Imagemagick convert by using it in conjunction with Ghostscript


Using the script below resulted in the Imagemagick convert taking 7.5 seconds, compared with the 2 pronged approach taking just over 0.5 seconds


#!/bin/bash


echo "---------------------------------------------------------"
echo "Use ImageMagick to convert PDF to 450x307 jpg"
echo "---------------------------------------------------------"

TT="$(date +%s%N)"

convert -resize 450x307 test.pdf test_resized.jpg

T="$(($(date +%s%N)-TT))"

# Seconds
S="$((T/1000000000))"

# Milliseconds
M="$((T/1000000))"

printf "Run time: %02d:%02d:%02d:%02d.%03d\n" "$((S/86400))" "$((S/3600%24))" "$((S/60%60))" "$((S%60))" "${M}"

echo "---------------------------------------------------------"
echo "Use Ghostscript to convert PDF to 450x307 jpg"
echo "---------------------------------------------------------"

TT="$(date +%s%N)"
gs -sDEVICE=jpeg -dNumRenderingThreads=4 -dNOGC -o test.jpg -dJPEGQ=95 test.pdf 2>&1
convert -resize 450x307 test.jpg test_resized.jpg
T="$(($(date +%s%N)-TT))"
# SecondsS="$((T/1000000000))"

# Milliseconds
M="$((T/1000000))"

printf "Run time: %02d:%02d:%02d:%02d.%03d\n" "$((S/86400))" "$((S/3600%24))" "$((S/60%60))" "$((S%60))" "${M}"
echo "---------------------------------------------------------"

Wednesday 10 April 2013

Upgrade Ghostscript and Imagemagick on a Centos server


The following script can be used to remove a previous instance of Ghostscript and Imagemagick from a Centos based server and re-install the current versions.


A manual upgrade is required as the Centos repo does not currently have Ghostscript v9 (which is required to use the inkcov device example here).

#!/bin/bash
yum remove imagemagick -y
yum remove ghostscript -y

cd /tmp


wget http://downloads.ghostscript.com/public/ghostscript-9.07.tar.gz
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz


tar xf ghostscript-9.07.tar.gz
tar xf ImageMagick.tar.gz


cd /tmp/ghostscript-9.07
autoconf
./configure
make
make install

cd /tmp/ImageMagick
autoconf
./configure --enable-shared \
           --with-modules \
           --with-perl \
           --with-x \
           --with-threads \
           --with-magick_plus_plus \
           --with-gslib \
           --with-wmf \
           --with-lcms \
           --with-rsvg \
           --with-xml \
           --without-dps
make
sudo make install
sudo ldconfig /usr/local/lib




cd /
rm -Rf /tmp/ghostscript-9.07
rm -Rf /tmp/ImageMagick

echo "Done!"










Counting the number of colour pages in a PDF with Ghostscript

Using Centos and Ghostscript (9.05 and above):

I am developing a print on demand solution at work and came up against an issue where I wanted to to be able to inspect a PDF file and work out where the colour pages appeared (if at all).

I am using Centos 5 as the production servers but this solution should work on any Linux system running Ghostscript 9.05 or above.

Ghostscript 9.05 is required as you need access to the inkcov device to be able to work out the ink coverage for each of the CMYK plates.

Now I had to make the assumption that if C=M=Y that the page had 4 colour grey/black on it rather than it being a colour image.  This may not be a 100% foolproof method but it was the only way I could think of determining if the page was colour or greyscale.

Anyway, here is the script I came with:


gs -o - -sDEVICE=inkcov test.pdf  | grep -v "^ 0.00000  0.00000  0.00000" | awk '{if($1~/Page/){lastpage=$2;} if($1!~/Page/ && $1~/0./ && ($1!=$2 && $1!=3) && $1!=0.00000){ print lastpage}}'

Basically the command queries the PDF and ignores the lines returned where CMY are 0.

AWK is then used to ignore the lines that are irrelevant and print only the page number for the pages which were determined to be colour.