【问题标题】:convert PDF into TIFF with 600dpi and jpg 96 dpi将 PDF 转换为 600dpi 和 jpg 96 dpi 的 TIFF
【发布时间】:2021-07-14 01:15:07
【问题描述】:

我想使用 ImageMagick 从 Python 脚本将 pdf 转换为 600 dpi 的 tiff 和 96 dpi 的 jpg。

我使用 (imagemagick) 命令行完成了这项任务,但我想在 python 中使用 Imagemagick 将 pdf 转换为 tiff 和 jpg,

你能帮我解决这个问题吗......

【问题讨论】:

    标签: python linux imagemagick


    【解决方案1】:

    首先您必须加载 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 
    

    【讨论】:

    • 但我只想使用 imagemagick 将 pdf 转换为 tiff 和 jpg,而不是使用 imagemagick (pythonmagick) 的 python 接口...
    • 所以我认为 Rakesh 的答案就是你要找的 :)
    • 我尝试添加 PyhtonMagick 库,但它不可用。有人能拿到吗?
    • @Amadeus:PythonMagick 是一个旧库,所以我添加了一个更新的替代品 PgMagick,它有更好的文档记录并且应该更容易安装。希望对您有所帮助。
    • @furins 我找到了另一个解决方案,但我会标记您的答案以供将来使用。感谢您的回复!
    【解决方案2】:

    您可以使用subprocess module来执行imagemagick命令

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多