【问题标题】:how to use imagemagick in a python script?如何在 python 脚本中使用 imagemagick?
【发布时间】:2025-12-10 17:50:02
【问题描述】:

尝试在 Python 脚本中使用 ImageMagick。我可以使用 Image Magic 代码将任何 pdf 文件转换为 PNG 图像,如下所示。这就是我想出的。

convert -density 300 -quality 90 input.pdf output.png

有没有办法将上面的代码convert -density 300 -quality 90 input.pdf output.png嵌入到python脚本中来将pdf转换为图像?

【问题讨论】:

  • 看看subprocess.run。也可以在 Python 中使用 Pillow 来完成,而不依赖于 imagemagick。

标签: python image pdf imagemagick-convert


【解决方案1】:

您可以通过 Python 中的子进程调用来做到这一点。

import subprocess

cmd = 'convert -density 300 -quality 90 input.pdf output.png'

subprocess.call(cmd, shell=True)

【讨论】: