I am converting various PDFs uploaded by end users into images using following command
-density 140 -limit memory 64MB -limit map 128MB [pdffile] page.png
Here is the result. On the right we have original PDF and on the left output image. As you can see the colors are quite noticeably different.
What could be causing this and how fix it?
Imagemagick provides the convert tool that can be used to do various complicated image processing tasks. Use convert to convert PDF pages to images with the following command: In the above command, we convert all the pages of the PDF files to images. -density is used to specify the DPI of the output images.
There is a quick and convenient way to convert PDF to one or more images. Command line tool ImageMagick does that (and a lot more). You can convert an entire PDF document to a single image, or, if you like, there is an option to output pages as a series of enumerated image files.
The number inside the bracket is used to select a page. Note that the page index starts at 0 instead of 1. To resize the converted image, you can supply the -resize option: You can also specify a range of pages with the following command: Convert multi-page PDFs to multiple images.
If you include a C-style integer format string, for example if you add %03d to the end of your output file name, you will get output_file_name-001.png, output_file_name-002.png, output_file_name-003.png, etc. This will actually convert page 2 of PDF to PNG, since numbering starts with 0.
try following command:
-density 140 -limit memory 64MB -limit map 128MB -colorspace RGB [pdffile] page.png
Edit: I later discovered that ImageMagick can do it fine, I just needed to use -colorspace sRGB
My final command was:
convert -density 560 -limit memory 64MB -limit map 128MB \
-colorspace sRGB [pdffile] -scale 25% page.png
The oversampling and scaling down was to counter the poor anti-aliasing mentioned below.
Before I discovered that, here was my earlier solution...
In my case the colors produced by ImageMagick's convert
were oversaturated, quite like those in the question. I was trying to convert this file using IM 6.7.7.10-6ubuntu3.
-resample 100
made no difference.
-colorspace RGB
seemed to produce more accurate saturations, but the entire image was darker than it should have been.
Curiously, this suggestion to use GhostScript instead of ImageMagick for the conversion, produced very close to the correct colors:
gs -q -sDEVICE=png16m -dSubsetFonts=true -dEmbedAllFonts=true \
-sOutputFile=page.png -r200 -dBATCH -dNOPAUSE [pdffile]
(The original suggestion passed the -dUseCIEColor
option, but in my case this appeared to reduce the gamma: light pixels were fine, but the dark pixels were too dark, so I removed it.)
After that, the only thing that bothered me was that the anti-aliasing/edges were a little off in places (especially visible on curves passing 45 degrees). To improve that, I created the output at four times the required resolution, and then scaled down afterwards, rendering those errors almost imperceptible. Note that I had to use ImageMagick's -scale
for this, and not -geometry
or -resize
, in order to avoid bicubic ringing effects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With