使用 ImageMagick 将 PDF 转换为 PNG

使用 ImageMagick,我应该使用什么命令来将 PDF 转换为 PNG?我需要最高质量,最小的文件大小。这就是我目前为止得到的(顺便说一句,非常慢) :

convert -density 300 -depth 8 -quality 85 a.pdf a.png

看看当用户“查看”PDF 时 Gmail 做了什么,质量非常棒,文件大小非常小。DPI 只有96(我必须设置密度为300才能得到像样的东西)。有人知道 GMail 是怎么做到的吗?谢谢。

147583 次浏览

when you set the density to 96, doesn't it look good?

when i tried it i saw that saving as jpg resulted with better quality, but larger file size

Reducing the image size before output results in something that looks sharper, in my case:

convert -density 300 a.pdf -resize 25% a.png
convert -density 192 input.pdf -quality 100 -alpha remove output.png

for pdf text document is good enough. -density 192 double 96dpi, higher just make bigger image and file size -quality 100 somehow this give slightly smaller file size -alpha remove to remove png transparent background

To get high quality, one should do "supersampling" in Imagemagick. Convert at a high density, but then resize down as needed (nominal enough to compensate for the high density).

convert -density 288 input.pdf -resize 25% output.png

288=72*4 (72 dpi is default density, so 4x)
25%=1/4

So the 1/4 compensates for the 4x.