将PDF转换为600dpi的TIFF和96dpi的JPG

2 投票
2 回答
11160 浏览
提问于 2025-04-17 15:51

我想用Python脚本把PDF文件转换成600 dpi的TIFF格式和96 dpi的JPG格式,使用的是ImageMagick这个工具。

我之前已经通过命令行完成了这个任务,但现在我想在Python中使用ImageMagick把PDF转换成TIFF和JPG格式。

你能帮我一下吗……

2 个回答

1

你可以使用 subprocess模块 来执行imagemagick命令。

import subprocess
params = ['convert', "Options", 'pdf_file', 'thumb.jpg']
subprocess.check_call(params)
3

首先,你需要加载一个围绕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 

撰写回答