【发布时间】:2021-07-14 01:15:07
【问题描述】:
我想使用 ImageMagick 从 Python 脚本将 pdf 转换为 600 dpi 的 tiff 和 96 dpi 的 jpg。
我使用 (imagemagick) 命令行完成了这项任务,但我想在 python 中使用 Imagemagick 将 pdf 转换为 tiff 和 jpg,
你能帮我解决这个问题吗......
【问题讨论】:
标签: python linux imagemagick
我想使用 ImageMagick 从 Python 脚本将 pdf 转换为 600 dpi 的 tiff 和 96 dpi 的 jpg。
我使用 (imagemagick) 命令行完成了这项任务,但我想在 python 中使用 Imagemagick 将 pdf 转换为 tiff 和 jpg,
你能帮我解决这个问题吗......
【问题讨论】:
标签: python linux imagemagick
首先您必须加载 imagemagick 库的包装器
使用PythonMagick:
from PythonMagick import Image
或使用pgmagick:
from pgmagick import Image
然后,独立于您加载的库,以下代码将转换和调整图像大小
img = Image()
img.density('600') # you have to set this here if the initial dpi are > 72
img.read('test.pdf') # the pdf is rendered at 600 dpi
img.write('test.tif')
img.density('96') # this has to be lower than the first dpi value (it was 600)
# img.resize('100x100') # size in px, just in case you need it
img.write('test.jpg')
# ... same code as with pythonmagick
【讨论】:
您可以使用subprocess module来执行imagemagick命令
import subprocess
params = ['convert', "Options", 'pdf_file', 'thumb.jpg']
subprocess.check_call(params)
【讨论】: