我一直在尝试使用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我已经尝试了几个不同的变体,有pixmap和没有,有overlay值设置和没有。PDF文件存在并且可以用Adobe Acrobat Reader打开,图像文件也存在-我尝试过PNG和JPG。
感谢您的帮助。
发布于 2018-08-14 17:17:04
下面是一些可以尝试的提示:
确保您的"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 pdfhttps://stackoverflow.com/questions/49340468
复制相似问题