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.


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)


for FILE in $(ls imgs/*.png|sort)
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")
COUNTER=$(echo "$COUNTER + 1" | bc)

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


Friday, January 25, 2008

New project - DDREditor

I've created a project on to host an app I've being toying since the new year break.

When it get to a point where it actually work it is supposed to be an Digital Didatic Resource Editor which aims to create and edit digital (and pre-press) cources using an integrated Learning Objects repository.

I'm using a small subset of docbook as my storage format and the app is being written in python with a gtk interface.

for more information go the the project's website or the project's Project Page at SF.

If anyone is interest on the subject please contact me.

brtcall - new app for download

I've written this app so my father could check his phone bills and do some crazy stuff with the data using excel. Since BRT (Brasil Telecom) only releases the local calls report as a pdf file I've written a small app that, using pdftotext, converts the PDF to a text file and extracts the values. It is written in python2.5, pygtk and users sqlite3 to store the data.

I must warn you: It is my first release and it is really buggy but it has been enough to help my father.