【问题标题】:Python PyMuPDF Fitz insertImagePython PyMuPDF Fitz insertImage
【发布时间】:2018-03-17 18:32:25
【问题描述】:

一直在尝试使用 PyMuPDF / Fitz 将图像放入 PDF 文件中,我在互联网上的任何地方都得到相同的语法,但是当我使用它时,我遇到了运行时错误。

>>> doc = fitz.open("NewPDF.pdf")
>>> page = doc[1]
>>> rect = fitz.Rect(0,0,880,1080)
>>> page.insertImage(rect, filename = "Image01.jpg")

error: object is not a stream
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\fitz\fitz.py", line 1225, in insertImage
return _fitz.Page_insertImage(self, rect, filename, pixmap, overlay)
RuntimeError: object is not a stream

>>> page
page 1 of NewPDF.pdf

我已经尝试了几种不同的变体,有像素图和没有像素图,设置了覆盖值,还有没有。 PDF文件存在,可以用Adobe Acrobat Reader打开,图片文件存在——我试过PNG和JPG。

感谢您的任何帮助。

【问题讨论】:

    标签: python image pdf-generation jpeg pymupdf


    【解决方案1】:

    只是一些尝试的提示:

    确保您的“Image01.jpg”文件已打开并使用完整路径。

    image_path = "/full/path/to/Image01.jpg" 
    
    image_file = Image.open(
        open(image_path, 'rb'))
        # side-note: generally it is better to use the open with syntax, see link below
        # https://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement
    

    为确保您确实位于预期的 pdf 页面上,请尝试此操作。此代码将仅在第一页插入图像

    for page in doc:
        page.InsertImage(rect, filename=image_path)
        break  # Without this, the image will appear on each page of your pdf
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      • 2022-11-24
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多