【问题标题】:Is it possible to convert an entire PDF file to a bunch images using ImageMagick (Ubuntu, Python)?是否可以使用 ImageMagick(Ubuntu,Python)将整个 PDF 文件转换为一堆图像?
【发布时间】:2020-01-17 09:47:50
【问题描述】:

我有一个 Python 应用程序,它可以将整个 PDF 文件转换为几个 jpg。 在 Pycharm 中是通过代码实现的:

import subprocess
subprocess.check_call(["magick",Platforma_IoT.pdf,Platforma_IoT.jpg], shell=True)  

Windows 10 上 Pycharm 中的代码完全可以正常工作,它将 pfd 的每一页转换为 jpg。但现在我想使用 Bash 控制台运行该代码。不幸的是,我在 Bash 中遇到错误:

import-im6.q16: unable to open X server `' @ error/import.c/ImportImageCommand/358.
./sd.py: line 3: syntax error near unexpected token `["magick","Platforma_IoT.pdf","Platforma_IoT.jpg"],'
./sd.py: line 3: `subprocess.check_call(["magick","Platforma_IoT.pdf","Platforma_IoT.jpg"], shell = True)'

我也试过那个代码:

import subprocess
subprocess.check_call(["convert","Platforma_IoT.pdf","Platforma_IoT.jpg"])

但是我在 bash 中遇到了同样的错误。

我只想运行代码,使用 bash 控制台将 PDF 的每一页转换为图像。我该怎么做?

【问题讨论】:

  • 在 bash 窗口中,只需键入:convert Platforma_IoT.pdf Platforma_IoT.jpg。那是假设你已经编译了 ImageMagick 并且 ImageMagick 的路径在你的 $PATH 环境变量中。否则,将 ImageMagick 的完整路径作为转换命令的一部分
  • @fmw42 我将 ImageMagick 添加到环境变量中,但它对我没有帮助。 “魔术:未找到”。如何在 bash 命令中直接传递 ImageMagick 路径?现在我只需输入“python3 name.py
  • 但是,我在添加环境变量时遇到了问题。我在“.bashrc”文件中添加了:`ImageMagick='/mnt/c/Program Files/ImageMagick-7.0.9-Q16 export ImageMagick 'export PATH=$PATH:ImageMagick/bin' 但 $ImageMagick 输出看起来像:- bash /mnt/c/Program: 没有这样的文件或目录。我试过 ImageMagick=/mnt/c/Program\ Files/ImageMagick7.0.9-Q16 但它不起作用
  • 好的,我使用了那个链接:tecmint.com/install-imagemagick-on-debian-ubuntu,它终于可以工作了。

标签: python bash imagemagick


【解决方案1】:

您遇到了错误的脚本解释。使用 shebang 定义正确的解释器:

#!/usr/bin/env python3

或者你可以直接在 python 中启动你的脚本:

python3 script.py

【讨论】:

  • 你是对的,但它仍然不起作用。 import subprocess subprocess.check_call(["magick","Platforma_IoT.pdf","Platforma_IoT.jpg"], shell=True) 在 PyCharm 中完美运行,但是当我在 Bash 控制台中运行它时会引发错误:Platforma_IoT.pdf: 1: Platforma_IoT.pdf: magick: not found Traceback [..] Command '['magick', 'Platforma_IoT.pdf', 'Platforma_IoT.jpg']' returned non-zero exit status 127
  • @MuscleUpUp 可能你有一个不同的环境,你没有解决所有的依赖关系。 IDE 可以为自己解决它,但不能为整个系统解决。在 Ubuntu 上使用 sudo apt install magic
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 2016-01-30
相关资源
最近更新 更多