10 Jul

Using ImageMagick to Generate Thumbnails

Having worked with web developers over time you need something to sometimes  perform certain tasks that would just take way to long if I did them manually. Imagine having thousands of PDF documents and now all the sudden you want to generate a thumbnail for all of them. Sure you can open up the PDF and take a screenshot of it which then can be shrunk to a smaller size. Now do that 5000 times.

If you are riding on a Linux box this is way easier then one might think. All you need to do the job is ImageMagick. This can be easily installed if you are on a Debian system like Ubuntu. Run the following command on Ubuntu to install ImageMagick.

  1. <span id="GRmark_dab4762dce19ca1b868aa165c4250a50c613429f_sudo:0" class="GRcorrect">sudo</span> apt-get install imagemagick


In a Rhel Based system such as Centos/Fedora you would run the follow command to install ImageMagick.

<span id="GRmark_baa50899861fa02f64ca984b10773e59babee914_yum:0" class="GRcorrect">yum</span> install <span id="GRmark_baa50899861fa02f64ca984b10773e59babee914_imagemagick:1" class="GRcorrect">imagemagick</span>

The size of the file is really small and should be no more than 740KB. Now let us pretend that we are in a directory with a PDF document and we want to create a thumbnail image of every page in that document.

  1. <span id="GRmark_039c7013318e9624181f69587a0f68fc050f9bf9_convert:0" class="GRcorrect">convert</span> -thumbnail x300 test.pdf test.png

The above example takes our PDF called test.pdf and creates a thumbnail with a height of 300 pixels of every page in the document. The files in the directory should auto increment so you are left with test-0.png test-1.png and so on. When adding an x infront of the size it means we are specifying the height. We could specify an exact dimension by using the command below:

  1. <span id="GRmark_0540a0c5b31a25807190e1c527a80fcb209c5cf3_convert:0" class="GRcorrect">convert</span> -thumbnail 400x300 test.pdf test.png

Now in the above command the thumbs will be 400 width and 300 height. If you wanted to just specify a width and keep the length proportional use the following command.

  1. <span id="GRmark_a8d89d2652e127bf9bdcdb605d238bce0150aba1_convert:0" class="GRcorrect">convert</span> -thumbnail 400 test.pdf test.png

Now without an x we are just saying make the width 400 pixels wide and the length whatever is proportional.

Now lets say we wanted to only create a thumbnail of the first page or the so called cover of the PDF. This can easily be done with the next command.

  1. <span id="GRmark_c79eb08c16864a937df4e9b655fb4b0b7598edfb_convert:0" class="GRcorrect">convert</span> -thumbnail 180 test.pdf<span id="GRmark_c79eb08c16864a937df4e9b655fb4b0b7598edfb_[:1" class="GRcorrect">[</span>0] test.png

The added on bracket zero bracket means first page. If you wanted the 23rd page you would put a 22 in the brackets. This now will generate a thumbnail from test.pdf using its cover that is exactly 180 pixels wide and the length is whatever is proportional.

I hope this helped. convert is a nice tool for Linux if you need to do on the fly conversions. The above commands can easily put inside a script that loops through all your PDFs but that is outside the scope of this article.