Search code examples
resolutionimagemagick-convertshrinkresize-image

Convert PDF to image with high resolution to fit in page


I regularly get tree-drilling-data out of a machine that should get into reports. The pdf-s contain too much empty space and useless information.

With convert i already managed to convert the pdf to png, cut out parts and rebuild an image i desire. It has a fine sharpness, its just too large: Output 1: Nice, just too large For my reports i need it in 45% size of that, or 660 pixels wide. The best output i managed up to now is this: Output 2: Perfect size but unsharp

Now, this is far away in quality from the picture before shrinking. For sure, i've read this article here, that already helped. But i think it must be possible to get an image as fine as the too large one in Output 1.

I've tried around for hours with convert -scale, -resize, -resample, playing around with values for density, sharpen, unsharpen, quality... nothing better than what i've got, using

convert -density 140 -trim input.pdf -quality 100 -sharpen 0x1.0 step1.png

then processing it to the new picture (output1, see up), that i'm putting to the correct size with

convert output1.png -resize 668x289! -unsharp 0x0.75+0.75+0.01 output2.png

I tried also "resize 668x" in order not to maybe disturb, no difference.

I find i am helpless in the end. I am not an IT-expert, i am a computer-affin tree-consultant. My understanding of image-processing is limited. Maybe it would make sense to stay on a vector-based format (i tried .gif and .svg ... brrrr).

I would prefer to stay with convert/imagemagick and not to install additional software.

It has to run from command-line, as it is part of a bash-script processing multiple files. I am using Suse Linux.

Grateful for your help!


Solution

  • In Imagemagick, set a higher density, then trim, then resize, then unsharpened. The higher the density, the sharper your result, but the slower it will get. Note that PNG quality of 100 is not the proper scale. It does not have quality values corresponding to 0 to 100 as in JPG. See https://imagemagick.org/script/command-line-options.php#quality. I cannot tell you the "best" numbers to use as it is image dependent. You can use some other tool such as at https://imagemagick.org/Usage/formats/#png_non-im to optimize your PNG output.

    So try,

    convert -density 300 input.pdf -trim +repage -resize 668x289 -unsharp 0x0.75+0.75+0.01 output.png
    

    Or remove the -unsharp if you find that it is not needed.

    ADDITION

    Here is what I get with

    convert -density 1200 P3_M002.pdf -alpha off -resize 660x -brightness-contrast -35,35 P3_M002.png
    

    enter image description here

    I am not sure why the graph itself lost brightness and contrast. (I suspect it is due to an imbedded image for the graph). So I added -brightness-contrast to bring out the detail. But it made the background slightly gray. You can try reducing those values. You may not need it quite so strong.