ImageMagick Convert 将 PDF 转换为 PNG 时添加空格

发布于 2025-01-13 07:42:52 字数 937 浏览 2 评论 0原文

我正在使用 ImageMagick 将以下 PDF 转换为 PNG 文件。

单击此处下载 PDF IMSLP (永久链接(如果直接下载已损坏)

在 PDF 查看器中看起来不错:

在此处输入图像描述

但使用 convert -密度 300 -background White -alpha off -alpha remove file.pdf /tmp/file.png 进行转换时,

图像会得到一个大白边:

输入图片描述这里

我不想事后修剪图像,我只是希望 ImageMagick 以某种方式尊重视口,或者无论如何,查看信息都被编码在 PDF 中。有谁知道哪个命令行参数可能会启用此行为?

编辑 10.03.2022:我在 Alpine Linux docker 镜像中使用 ImageMagick 7.1.0.16 和 Ghostscript 9.55.0。

I'm using ImageMagick to convert the following PDF to an PNG file.

Click here to download the PDF from IMSLP (Permalink if the direct download is broken)

In a PDF viewer it looks nice:

enter image description here

but when converting with convert -density 300 -background white -alpha off -alpha remove file.pdf /tmp/file.png

the image gets a large white margin:

enter image description here

I do not want to trim the image afterwards, I just want ImageMagick to somehow respect the view-port or however that viewing information is being encoded in the PDF. Does anyone know which command-line parameter might enable this behavior?

Edit 10.03.2022: I'm using ImageMagick 7.1.0.16 with Ghostscript 9.55.0 inside an Alpine Linux docker image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2025-01-20 07:42:52

KenS 的提示正是我所寻找的 - PDF 定义了 ImageMagick 7.1.0 未使用的 CropBox默认。因此,解决方案是修改命令以包含以下 -定义信息:

convert -define pdf:use-cropbox=true file.pdf /tmp/file.png 

谢谢大家的帮助!

The hint from KenS was exactly what I was looking for - the PDF defines a CropBox that ImageMagick 7.1.0 was not using by default. The solution therefore is to modify the command to include the following -define information:

convert -define pdf:use-cropbox=true file.pdf /tmp/file.png 

Thank you all for your help!

假面具 2025-01-20 07:42:52

我在使用 Ghostscript 9.54 的 ImageMagick 6.9.12-42 中没有获得额外的余量。但改变密度似乎没有任何效果。

convert -density 300 -background white file.pdf[1] x2.png

输入图片此处描述

问题可能是 PDF 格式错误。它是如何创建的?另外您使用的是哪个版本的 Ghostscript?可能是GS版本问题。

如果这是一个扫描的 PDF,它是矢量 PDF 外壳中的光栅图像,那么您可以只使用 pdfimages 来提取光栅文件。请参阅 https://manpages.debian.org/testing/poppler -utils/pdfimages.1.en.html

I do not get your extra margin in ImageMagick 6.9.12-42 using Ghostscript 9.54. But changing the density does not seem to have any effect.

convert -density 300 -background white file.pdf[1] x2.png

enter image description here

The issues may be a malformed PDF. How was it created? Also what version of Ghostscript are you using? It could be a GS version issue.

If this was a scanned PDF that is a raster image in a vector PDF shell, then you could just use pdfimages to extract the raster files. See https://manpages.debian.org/testing/poppler-utils/pdfimages.1.en.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文