37

After upgrading ImageMagick text quality got degraded when convert pdf to jpeg:

Old image enter image description here

New Image enter image description here Conversion command: convert foo.pdf foo.jpeg

Old ImageMagick version:

[root@home]#  convert -version
Version: ImageMagick 6.2.8 05/07/12 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html
Copyright: Copyright (C) 1999-2006 ImageMagick Studio LLC

generated files size:

-rw-r--r-- 1 root root 139K Apr  2 16:11 foo-0.jpeg
-rw-r--r-- 1 root root 130K Apr  2 16:11 foo-1.jpeg
-rw-r--r-- 1 root root 334K Mar 24 14:27 foo.pdf

After upgrading ImageMagick

[root@home]#  convert -version
Version: ImageMagick 6.7.8-10 2012-08-17 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

generated files size:

-rw-r--r-- 1 root root  60K Apr  2 16:11 foo-0.jpeg
-rw-r--r-- 1 root root  55K Apr  2 16:11 foo-1.jpeg
-rw-r--r-- 1 root root 334K Mar 24 14:27 foo.pdf

I've tried using antialias flag:

convert -antialias  foo.pdf foo.jpeg

Which did nothing, I've tried setting an higher quality:

convert -quality 100 foo.pdf foo.jpeg

and super sampling:

convert -density 288 -background white -alpha off foo.pdf -resize 25%  foo.jpeg

both gave bigger files and better results, but ran more time and had lower quality that the old ImageMagick version.

any advises?

Link to the file

Simon Arnold
  • 15,849
  • 7
  • 67
  • 85
Kuf
  • 17,318
  • 6
  • 67
  • 91
  • Can you provide a download link to the sample file for me to test? – likeitlikeit May 05 '13 at 18:40
  • I've added a link to the end of the question, thanks – Kuf May 05 '13 at 18:56
  • Tried using a later version of ImageMagick? $ convert --version Version: ImageMagick 6.7.7-10 2012-08-17 Q16. This is on Linux Mint Nadia – Atle May 09 '13 at 06:35
  • Also have a look at this: http://stackoverflow.com/questions/6605006/convert-pdf-to-image-with-high-resolution – Atle May 09 '13 at 06:44
  • thanks @Atle, I've tried that already, the answer of potatoe worked like a charm! – Kuf May 09 '13 at 07:07
  • Great! But curious as I am I had to google, and it seems delegate.xml was changed for a reason: http://www.imagemagick.com/discourse-server/viewtopic.php?t=18184&p=69472#p69445 Just so you're aware. – Atle May 09 '13 at 07:14
  • thanks for the heads up! will send it to QA next week, will update on any upcoming issues. – Kuf May 09 '13 at 15:44
  • 2
    You want `-alpha remove` instead of `-alpha off`. – Skippy le Grand Gourou Sep 26 '16 at 07:42

4 Answers4

38

I see the same problem with your sample file. It looks like ImageMagick's delegates for the PDF conversion may have changed with the new install.

If you try convert -verbose foo.pdf foo.jpeg, do you see -sDEVICE=pngalpha in the command that gets sent to gs? The pnmraw device has been used in the past, and switching back to that seems to fix the problem for me.

In ImageMagick's delegates.xml file (which may be in /etc/ImageMagick, but could be somewhere else depending on your setup), look for the decode="ps:alpha" delegate line and change -sDEVICE=pngalpha in the command to -sDEVICE=pnmraw. (You can probably just search for pngalpha in the file.)

potatoe
  • 1,070
  • 9
  • 14
  • 1
    Glad to hear it. PDF conversion in ImageMagick can be a big pain, but it's great when it works. – potatoe May 09 '13 at 07:07
  • 1
    People who want to use this solution should be aware that delegate.xml was changed for a reason, so that they don't create other problems for themselves: http://www.imagemagick.com/discourse-server/viewtopic.php?t=18184&p=69472#p69445 – Atle May 09 '13 at 07:18
  • 7
    Thanks for the link, it's a good point. That comment also brings up something I considered, but forgot to include in the answer: you could just use ghostscript directly to do the PDF conversion, although it's a little less friendly to use. Something like this: `gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150 -sOutputFile=foo-%d.jpg foo.pdf` – potatoe May 09 '13 at 16:10
  • This was killing me! You nailed it on the head. Whatever drawbacks there may be with this hack, I say bring 'em on! – PatrickT Feb 06 '14 at 22:49
  • 3
    I found simply adding `-flatten` resolves the issue without needing to edit *delegates.xml*. `convert image.png -flatten image.jpg` – newtriks Feb 08 '14 at 17:13
  • @potatoe answer worked for me after tying other solutions. Trick was: **-dAlignToPixels=0** – Meetai.com Jul 05 '14 at 15:38
  • 2
    Looks like the issue comes from dealing with alpha, in which case another (tested) option is to use `-alpha remove`. The `-flatten` option suggested by newtricks also works. – Skippy le Grand Gourou Sep 26 '16 at 07:46
  • this didn't work for me in 2022 – elig Apr 26 '22 at 10:16
  • @potatoe `-dTextAlphaBits=4` did a trick for me. Other options (probably) don't play any role. @elig Try to [download older GS version from some mirror](https://ftp.icm.edu.pl/packages/ghostscript/old-gs-releases/gs904w64.exe), i.e. I use 9.04. – dma_k Jan 23 '23 at 21:38
13

it seem that problem at DPI. when convert pdf, imagemagick using Ghostscript. you can skip using imagemagick.

$ gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dGridFitTT=2 -dUseCropBox -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r200x200 -sDEVICE=jpeg -dJPEGQ=100 -sOutputFile=foo-%05d.jpg foo.pdf

set -r option higher value. Ghostscript have default value is 100DPI.

or using convert option -density. this option set pdf converted DPI.

$ convert -density 200x200 foo.pdf foo.jpg
c2o93y50
  • 211
  • 2
  • 4
0

PDF files are vector files and have no specific size. Their size is controlled by defining the density and units before reading in the PDF file. You can get better quality for the same desired output file size by supersampling. That means rasterize the PDF to a large size and then resize to your desired actual size. For example in ImageMagick:

convert -units pixelsperinch -density 288 image.pdf -resize 25% output.jpg

The nominal density if left off is 72 dpi. So 72*4=288. Then resize by 1/5=25% gets back to the same default size, but should look much better. Change the density or resize to deal with quality and final size as desired.

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

The simplest way is one of the solutions you suggested yourself.

You just have to use +antialias to disable the addition of antialiasing edge pixels instead of -antialias.

convert +antialias foo.pdf foo.jpeg

Documentation

Baras
  • 159
  • 1
  • 6