merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

I have a large number of jpg files that I need to ‘manipulate’ and then save as pdf files. The jpg files are images of line drawings.
I am using the Perl version of ImageMagick and all works well except…
The exception is the size of the pdf file. The pdf version of the image tends to be larger size on the disc than the jpg version.
However when I used the IRIS application the pdf image is typically a qurter or fifth of the size. IRIS cannot be used ‘automatically’ therefore this possible solution cannot be implemented.
I do not want to reduce the resolution of the image.
Can ImageMagick be used in a way to give a similar size reduction? If not are there other Perl based ‘tool’s that I can use to get the reduced size?
  • Comment on Size problem of pdf file using ImageMagick

Replies are listed 'Best First'.
Re: Size problem of pdf file using ImageMagick
by jf1 (Beadle) on Dec 23, 2015 at 18:37 UTC

    pdf output can be compressed in different ways. Newer versions of ImageMagick seem to reraster the image instead of just embedding the original jpg, and then output it without compression. Try to use compression method jpeg for the output and quality level 75%

    As you say, your images are line drawings, you may also prefer to reduce number of colors to 2 (black and white) and then save them using some lossless compression scheme instead (LZW etc) instead of jpeg

    Maybe you also would like to switch to graphicsmagick instead, (also provides Perl bindings) the default settings of which I generally prefer (in particular w.r.t. filesize, without visibly compromising quality)

      I have obtained these images by scanning the drawings in just two colours.
      I see your comments about the alternate graphicsmagick but I would prefer to stick with imagemagick.
      Using comments from both replies I altered the Perl so that the jpg and pdf were saved as before and then saved again using LZW compression and a quality of 75. This gave four files. The code follows
      $mod_file_name = $file_name_jpg; $mod_dp = '-' . $jv . 'pc.'; # normal $mod_file_name =~ s/\./$mod_dp/; $rc = $img_copy->Write($dir_name . '\\' . $mod_file_name); $mod_file_name =~ s/jpg/pdf/; $rc = $img_copy->Write($dir_name . '\\' . $mod_file_name); # compressed $mod_file_name = $file_name_jpg; $mod_dp = '-' . $jv . 'pc-compressed.'; $mod_file_name =~ s/\./$mod_dp/; $rc = $img_copy->Write(filename => $dir_name . '\\' . $mod_file_name, +compression => 'LZW', quality => 75); $mod_file_name =~ s/jpg/pdf/; $rc = $img_copy->Write(filename => $dir_name . '\\' . $mod_file_name, +compression => 'LZW', quality => 75);
      For one jpg file I obtained the four files for:
      1. The original jpg file
      2. After using Scale to reduce the size of the image by 2 (in x and y)
      The file sizes were for full size
      jpg 1,099 kb
      pdf 1,331 kb
      jpg compressed 742 kb
      pdf compressed 2,144 kb
      The file sizes were for half size
      jpg 304 kb
      pdf 660 kb
      jpg compressed 304 kb
      pdf compressed 660 kb
      This was not what I was expecting since:
      For full size – although the compressed jpg was smaller than the uncompressed version, the compressed pdf was nearly twice as large as the uncompressed version
      For half size –there was no change between the compressed and uncompressed version.
      Does it make sense that the compressed pdf is larger than the original and why does compression seem to have effect after scale had been used on the original image?
Re: Size problem of pdf file using ImageMagick
by kennethk (Abbot) on Dec 23, 2015 at 16:08 UTC