0

次のコードを使用して複数の tiff 画像を 1 つの 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しかし、呼び出しから次のエラー メッセージが表示されます:
Invalid Parameter - "G:\Reonomy\ACRIS\TitleDocumentsDownload\Output\QN_15_65\2009033100558001\2.tiff"

そして、Windows のコマンドラインでまったく同じコマンドを実行すると、PDF ファイルが正常に作成され、次の警告メッセージが表示されます
。`TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/824.

なぜこれがPythonで起こっているのかわかりません。迅速な解決策をいただければ幸いです。

4

2 に答える 2

2

これは、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()
于 2016-02-12T21:26:48.887 に答える
0

これは私にとってはうまくいきます:

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 ユーティリティであるためです。このリンクを読んでください。ImageMagick フォルダーからコマンドラインを実行していますか?

最も簡単な解決策は、convert.exe ファイル (ImageMagick) の名前を別のもの (convertMagick.exe など) に変更し、os.system パラメータで同じものを使用することです。

于 2013-09-18T12:16:59.707 に答える