首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >批量转换TIFF图像为PDF ImageMagick Python

批量转换TIFF图像为PDF ImageMagick Python
EN

Stack Overflow用户
提问于 2013-09-18 20:03:51
回答 2查看 8.4K关注 0票数 0

我正在尝试使用以下代码将多个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中发生。任何快速的解决方案都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2016-02-13 05:26:49

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-09-18 20:17:00

这对我来说很好用:

代码语言:javascript
复制
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参数中使用相同的名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18871778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档