【问题标题】:Batch convert TIFF images to PDF ImageMagick Python将 TIFF 图像批量转换为 PDF ImageMagick Python
【发布时间】:2013-09-18 12:03:50
【问题描述】:

我正在尝试使用以下代码将多个 tiff 图像转换为一个 PDF 文件,但它不起作用。 os.system('convert "G:\xyz\abc\TitleDocumentsDownload\Output\abc\2009033100558001\1.tiff" "G:\xyz\abc\TitleDocumentsDownload\Output\abc\2009033100558001\2.tiff" "G:\xyz\abc\TitleDocumentsDownload\Output\abc\2009033100558001\3.tiff" "G:\xyz\abc\TitleDocumentsDownload\Output\abc\PDFs\2009033100558001.pdf"')

但我从 os.system 调用中收到以下错误消息:
无效参数 - "G:\Reonomy\ACRIS\TitleDocumentsDownload\Output\QN_15_65\2009033100558001\2.tiff"强>

当我在 Windows 的命令行上运行完全相同的命令时,会成功创建 PDF 文件,并显示以下警告消息:
convert.exe:遇到标签为 33000 (0x80e8) 的未知字段。 `TIFFReadDirecto ry'@warning/tiff.c/TIFFWarnings/824.

我不知道为什么在 Python 中会发生这种情况。任何快速的解决方案将不胜感激。

【问题讨论】:

    标签: python python-2.7 pdf-generation tiff imagemagick-convert


    【解决方案1】:

    这是我创建的一个不依赖 ImageMagick 的纯 Python 实现。它只依赖于 PIL 和 reportlab。它可以在 Google App Engine 等受限环境中运行。

    def TIFF2PDF(tiff_str, max_pages = 200):
      '''
      Convert a TIFF Image into a PDF.
    
      tiff_str: The binary representation of the TIFF.
      max_pages: Break after a number of pages. Set to None to have no limit.
      '''
      import PIL
      import reportlab
      import reportlab.lib.pagesizes as pdf_sizes
      from cStringIO import StringIO
      logging.info("TIFF2PDF")
    
      # Open the Image in PIL
      tiff_img = PIL.Image.open(StringIO(tiff_str))
    
      # Get tiff dimensions from exiff data. The values are swapped for some reason.
      height, width = tiff_img.tag[0x101][0], tiff_img.tag[0x100][0]
    
      # Create our output PDF
      out_pdf_io = StringIO()
      c = reportlab.pdfgen.canvas.Canvas(out_pdf_io, pagesize = pdf_sizes.letter)
    
      # The PDF Size
      pdf_width, pdf_height = pdf_sizes.letter
    
      # Iterate through the pages
      page = 0
      while True:
        try:
            tiff_img.seek(page)
        except EOFError:
            break
        logging.info("Converting tiff page: %s"%page)
        # Stretch the TIFF image to the full page of the PDF
        if pdf_width * height / width <= pdf_height:
          # Stretch wide
          c.drawInlineImage(tiff_img, 0, 0, pdf_width, pdf_width * height / width)
        else:
          # Stretch long
          c.drawInlineImage(tiff_img, 0, 0, pdf_height * width / height, pdf_height)
        c.showPage()
        if max_pages and page > max_pages:
          logging.error("Too many pages, breaking early")
          break
        page += 1
    
      logging.info("Saving tiff image")
      c.save()
      return out_pdf_io.getvalue()
    

    【讨论】:

      【解决方案2】:

      这对我很有效:

      import os
      os.system('convert G:\xyz\abc\TitleDocumentsDownload\Output\abc\2009033100558001\1.tiff G:\xyz\abc\TitleDocumentsDownload\Output\abc\2009033100558001\2.tiff G:\xyz\abc\TitleDocumentsDownload\Output\abc\2009033100558001\3.tiff G:\xyz\abc\TitleDocumentsDownload\Output\abc\PDFs\2009033100558001.pdf')
      

      你能试试看是否有错误吗? 你是在 linux 机器上运行第一个命令吗?

      这可能是因为 convert 是一个用于更改文件系统的 Windows 实用程序。阅读this 链接。你是从 ImageMagick 文件夹运行命令行吗?

      最简单的解决方案是将 convert.exe 文件 (ImageMagick) 重命名为其他名称,例如 convertMagick.exe,然后在 os.system 参数中使用相同的名称。

      【讨论】:

      • 不,我在 Windows 7-64bit 上运行它
      • 如果你在 linux 上工作,你可以使用os.system('tiff2pdf -o test.pdf test.tiff')。您可能需要先安装它。
      猜你喜欢
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2011-06-10
      相关资源
      最近更新 更多