Saturday, January 26, 2008

Magick from the command line

Last night my girlfriend told me that she needed to convert an PDF file into a PNG sequence, crop it and regenerate the PDF. She, a biology teacher, was planning on taking screenshots of the PDF files and crop it using the Gimp. She never told me how she was going to recreate the PDF file though.

Being an true hacker, in the sense that I can't see an unsolved problem, I decied to post a better way of doing it so she can read it from anywhere she is, as long as internet access is not a problem.

Here it goes:
We will be using the folowing plataform:
And the simple steps:

1 - install imagemagick if not already installed.

If the command "dpkg --list | grep imagemagick" returns anything ImageMagick is already installed and you can skip this part.
Use synaptic, aptitude or apt-get to install. In the example I'll use apt-get because it is what I usually use (old habits die hard)
apt-get install imagemagick

2 - convert PDF file into an sequence of PNG files

convert BrOoZine006.pdf pg-%03d.png

3 - crop the images:

Since I'll be croping the image to half it's size I'll ask for the image size:
identify -format "%wx%h" pg-000.png
convert -crop 595x421+0+0 pg-000.png out.png

4 - reasemble the pdf file from it's parts

convert *.png final.pdf

5 - Script

And here we have a functional script that takes as parameters the name of the original PDF and the new PDF file.
#!/bin/bash

PDF_IN=$1
PDF_OUT=$

mkdir imgs croped

convert $PDF_IN imgs/pg-%03d.png

WIDTH_ORI=$(identify -format "%w" imgs/pg-000.png)
HEIGHT_ORI=$(identify -format "%h" imgs/pg-000.png)

WIDTH_NEW=$(echo "$WIDTH_ORI / 2" | bc)

COUNTER=0

for FILE in $(ls imgs/*.png|sort)
do
NEWFILE=$(printf "%03d" $COUNTER)
DESTFILE=$(echo $FILE| sed -r "s#imgs/.*\$#croped/$NEWFILE.png#g")
convert -crop $WIDTH_NEW"x"$HEIGHT_ORI"+0+0" $FILE $DESTFILE
COUNTER=$(echo "$COUNTER + 1" | bc)
NEWFILE=$(printf "%03d" $COUNTER)
DESTFILE=$(echo $FILE| sed -r "s#imgs/.*\$#croped/$NEWFILE.png#g")
convert -crop $WIDTH_NEW"x"$HEIGHT_ORI"+"$WIDTH_NEW"+0" $FILE $DESTFILE
COUNTER=$(echo "$COUNTER + 1" | bc)
done

convert croped/*.png $PDF_OUT.pdf

rm -fr imgs croped

Copy the script above, paste it into a text file and then execute:
bash scriptname pdf_file new_pdf_file


resources:

No comments: