How do I minimize IM's PDF output size?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
silenceseeker

How do I minimize IM's PDF output size?

Post by silenceseeker »

I am trying to convert a JPG image to PDF, using convert:
convert lettersizepage_at_300_dpi.jpg lettersizepage_at_300_dpi.jpg.pdf
It successfully converts that JPG image to a single page PDF file, but that PDF file has a HUGE size...

The input JPG file is about 1MB in size. The output PDF is 20MB in size!

I tried using the '-compress JPEG' option but that didn't help a bit. :(

Is there a way to reduce ImageMagick's PDF output to a sane size?

I am aware of the image quality tradeoff - I only need an image of about the same quality of the original JPG file.

>>>>>>>>>>>>>>>>>> Update: I just tested this again and I got baffling results:

On my home Linux box, running ImageMagick 6.2.2 05/24/06 Q16, the output PDF file size is about the same as the input JPG.

But on my web host's (Linux) server, running ImageMagick 6.4.2 06/27/08 Q16, the output PDF files is about 20X larger than the same input JPG!

This is using the same exact command and JPG input file!

What could explain this?

Is it possible that the web host configured ImageMagick so that it does not compress? Is it possible at all to maim ImageMagick like this?


Thanks :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I minimize IM's PDF output size?

Post by fmw42 »

I have noticed that IM PDF files are much larger than other applications. But I suspect IM relies upon ghostscript to handle this and so may be out of IM control. I would be interested in hearing what the IM folks have to say, also.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: How do I minimize IM's PDF output size?

Post by magick »

Using the latest ImageMagick release:
  • -> convert logo: logo.jpg

    -> convert logo.jpg logo.pdf

    -> ls -l logo.jpg logo.pdf

    -rw------- 1 magick magick 33572 2008-08-27 14:00 logo.jpg
    -rw------- 1 magick magick 274216 2008-08-27 14:00 logo.pdf

    -> convert logo.jpg -compress jpeg logo.pdf

    -> ls -l logo.jpg logo.pdf

    -rw------- 1 magick magick 33572 2008-08-27 14:00 logo.jpg
    -rw------- 1 magick magick 37317 2008-08-27 14:00 logo.pdf
silenceseeker

Re: How do I minimize IM's PDF output size?

Post by silenceseeker »

Magick, thank you very much for your quick answer. I hope that you noticed in my original posting that I, too, was able to convert from JPG to PDF, keeping the file size to about the same.

However, what I also described there is that on a different system (my web host's, on which I don't have a root account, only a jailshell, ImageMagick behaves differently (as described above).

So, my question remains: Is it possible that the web host configured ImageMagick so that it does not compress? Is it possible at all, for a system administrator, to cripple ImageMagick like this?

Thank you!
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: How do I minimize IM's PDF output size?

Post by rmagick »

It seems more likely that your web host is using an older version of ImageMagick. You can use this command to find out what version is installed.

Code: Select all

convert -version
silenceseeker

Re: How do I minimize IM's PDF output size?

Post by silenceseeker »

rmagick wrote:It seems more likely that your web host is using an older version of ImageMagick.
Thanks for your tip. However, as I mentioned in my original posting, my web host is actually using a newer version than the one on my Fedora Core 4 Linux box (and which is working beautifully). My web host is running ImageMagick 6.4.2 06/27/08 Q16.

Any ideas what could be going wrong?

At some point I thought that perhaps my web host (shared hosting) intentionally configured ImageMagick this way, so that users will not use the "resource intensive" (as web hosts like to put it) compression.

But then I checked and saw that compression for .JPG or .PNG is enabled in that web host's ImageMagick. Compression is not working only for the PDF format. Weird. :shock:
thzxcyl

Re: How do I minimize IM's PDF output size?

Post by thzxcyl »

Hello, I also have the same problem where jpg are not compressed when converted to pdf, resulting about 20X size than the original jpg. Both 6.4.4-Q16 and 6.4.1-Q16 have the same problem. Any idea how to fix this?

Many thanks!
thzxcyl

Re: How do I minimize IM's PDF output size?

Post by thzxcyl »

Oh, and I'm on a Windows box using the binaries from the imagemagick website. The static version does not detect the existence of gs on my computer (i.e. does not convert to pdf at all), and the dynamic version is giving me this:

~
$ /cygdrive/c/Program\ Files/ImageMagick-6.4.4-Q16/convert.exe logo: logo.jpg

~
$ /cygdrive/c/Program\ Files/ImageMagick-6.4.4-Q16/convert.exe logo.jpg logo.pdf

~
$ ls -l logo.*
-rwxrwxrwx 1 user None 33572 Oct 11 17:20 logo.jpg
-rwxrwxrwx 1 user None 274209 Oct 11 17:20 logo.pdf

(yeah it is cygwin bash shell, but my im is not the cygwin version)

Thanks for the help!
m_stolle

Re: How do I minimize IM's PDF output size?

Post by m_stolle »

Any update on this issue? The new version has made it into Ubuntu and it's really annoying. Old versions of ImageMagick would move the jpeg as a jpeg-compressed image into the PDF. New versions seem to re-raster, making for huge pdfs.

Martin
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: How do I minimize IM's PDF output size?

Post by magick »

We cannot reproduce the problem:
  • -> convert logo: -sharpen 0x1 logo.pdf
    -> ls -l logo.pdf
    -rw-r--r--. 1 cristy cristy 143297 2009-05-25 16:42 logo.pdf
    -> convert logo: -sharpen 0x1 -compress jpeg logo.pdf
    -> ls -l logo.pdf
    -rw-r--r--. 1 cristy cristy 53627 2009-05-25 16:42 logo.pdf
In logo.pdf we see
  • /Filter [ /DCTDecode ]
Which means the image is JPEG-compressed as expected.
m_stolle

Re: How do I minimize IM's PDF output size?

Post by m_stolle »

Hm, appears that it must've been fixed then. It's not listed in the Changelog anywhere. I get the following:

mstoll@MartinVaioTX:~$ convert logo: -sharpen 0x1 logo.pdf
mstoll@MartinVaioTX:~$ ls -l logo.pdf
-rw-r--r-- 1 mstoll mstoll 263214 2009-05-25 23:03 logo.pdf
mstoll@MartinVaioTX:~$ strings logo.pdf | grep Filter
/Filter [ /RunLengthDecode ]
/Filter [ /RunLengthDecode ]
/Filter [ /RunLengthDecode ]

mstoll@MartinVaioTX:~$ convert --version
Version: ImageMagick 6.4.5 2009-01-22 Q16 OpenMP http://www.imagemagick.org

So somewhere between 6.4.5 and current, it must've been fixed. Oh well, too bad it wasn't fixed before Ubuntu 9.04 was frozen 8-(. I'll try compiling Ubuntu packages from Debian unstable (6.5.1) - hopefully that'll tie me over until Ubuntu 9.10.

Martin
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How do I minimize IM's PDF output size?

Post by anthony »

Look at the table of compression methods and the PDF results in the raw notes on IM Exmaples
http://www.imagemagick.org/Usage/formats/#pdf
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
m_stolle

Re: How do I minimize IM's PDF output size?

Post by m_stolle »

Hmm... thanks for the link to the notes. The -compress might have helped me with the older version of IM, however the 6.5.1 version from Debian unstable works out-of-the-box, so I venture it was a regression in IM, since jpg-to-pdf worked as expected in old versions and new versions - just not in-between. I had previously searched for documentation of -compress but not found it 8-/.

I still get a segfault when trying to convert multiple jpgs to a multipage pdf, but that's ok - I converted them one-by-one and then used pdfjoin from pdfjam to merge the pages.

Martin
Post Reply