0

I'm trying to convert multiple tiff images to one PDF file using the following code but it's not working. 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"')

But I'm getting the following error message from the os.system call:
Invalid Parameter - "G:\Reonomy\ACRIS\TitleDocumentsDownload\Output\QN_15_65\2009033100558001\2.tiff"

And when I run the exactly same command on command-line in windows, PDF file is successfully created with the following warning message:
convert.exe: Unknown field with tag 33000 (0x80e8) encountered. `TIFFReadDirecto ry' @ warning/tiff.c/TIFFWarnings/824.

I don't know why this is happening in Python. Any quick solution would be appreciated.

Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
Sibtain Norain
  • 679
  • 2
  • 15
  • 25

2 Answers2

2

Here is a pure python implementation I whipped up that does not rely on ImageMagick. It only relies on PIL and reportlab. It can run on restricted environments like 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()
speedplane
  • 15,673
  • 16
  • 86
  • 138
0

This works well for me:

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')

Can you try and see if there is an error? Are you running the first command on a linux machine?

This could be happening because convert is a Windows utility for changing filesystems. Read this link. Are you running the command-line from the ImageMagick folder?

The simplest solution would be to rename the convert.exe file(ImageMagick) to something else, say convertMagick.exe, and then using the same in the os.system parameter.

Sahil M
  • 1,790
  • 1
  • 16
  • 31