我正在尝试使用以下代码将多个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中发生。任何快速的解决方案都将不胜感激。
发布于 2016-02-13 05:26:49
这是我创建的一个纯python实现,它不依赖于ImageMagick。它只依赖于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()
发布于 2013-09-18 20:17:00
这对我来说很好用:
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参数中使用相同的名称。
https://stackoverflow.com/questions/18871778
复制相似问题