6

i want to convert a PDF to PNG images using convert. The images must fit the 1920x1080 ratio by having a ?x1080 ratio, and have the best quality.

Here are many options i can use with convert : https://imagemagick.org/script/command-line-options.php#append


  • First i tried the following command line :

convert my.pdf -geometry 1920x1080 -size 1920x1080 -density 1920x1080 my_resized_pdf.png

The result of the command gives me an image with the good geometry (763x1080), but a low quality i don't want to get.

enter image description here

  • I use convert command line without the geometry parameter as following :

convert my.pdf -size 1920x1080 -density 1920x1080 my_resized_pdf.png

The quality of the result is exactly what i want but the resolution is not 1920x1080 ratio, but 842x595. Its does not exactly fit on height the 1920x1080 ratio.

enter image description here

Is it possible to get PNG images with a ?x1080 ratio and with a 100% quality from a PDF ? Or is 842x595 the biggest ratio to get a 100% quality image ? Should i set a DPI option to convert ?

6

This involves some trial & error and in the end, it's debatable which result you might consider to be the "best result". So allow me to just give some generic advice:

  • use the -flatten option to get rid of transparent background. The transparency makes it hard to judge actual quality of the result. If you need the transparency in the final image, you can remove -flatten once you're sure of the quality.

  • use something like -density 300 to get a high DPI result. The main issue with convert is that it uses a very low density by default (72 DPI). This parameter has to be specified before the input file.

  • downscaling a high DPI image might cause additional blur, so perhaps calculating the correct DPI value to achieve the desired resolution is the way to go:

    $ convert -density 100 file.pdf -flatten file100.png
    $ file file100.png
    file100.png: PNG image data, 827 x 1169, 8-bit colormap, non-interlaced
    $ echo $((1080*10000/1169))
    9238
    $ convert -density 92.38 file.pdf -flatten file9238.png
    $ file file9238.png
    file9238.png: PNG image data, 764 x 1080, 8-bit colormap, non-interlaced
    

I'm not sure if there is a way to have convert determine "ideal" DPI value by itself.

If you take this question to the ImageMagick IRC channel or forum, I'm sure you'd get some more advice. It helps if you provide the link to the PDF file you're working with. ;)

You can also improve quality in other ways, for example by trimming empty borders away. You're losing a lot of resolution if half of the page is white. There are even solutions that re-wrap PDF text to get the most out of available screenspace (e.g. k2pdfopt).

Finally, also try other programs. This is a matter of opinion, but I prefer using Inkscape or GhostScript directly. ImageMagick has characters "glued together", Inkscape has a more balanced result, and GhostScript allows you to render a blur-free pure pixel image (if that's something you like - use pngalpha for the blurry version, which is virtually identical to convert).

ImageMagick:

ImageMagick

Inkscape:

Inkscape

GhostScript:

gs -r92.38 -sDEVICE=png48 -sOutputFile=ghostscript.png file.pdf

GhostScript

  • 1
    Good answer! +1; for the sake of completion could you please test the performance of pdftoppm -q -aa no -png -scale-to 1080 y.pdf out ? – JJoao Oct 22 '18 at 17:00
1

Try:

convert -resize 1920x1080  in.pdf   out.png

If your pdf has 10 pages you will get 10 files out-1.png ... out-10.png

Please ignore this answer: although useful for raster files resizing, it is producing very blurry result for PDF files (low DPI upscaled).

  • 3
    this gives a very blurry result (low DPI upscaled) – frostschutz Oct 22 '18 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.