如何修复 AttributeError:“Image”对象没有“seek”属性。您是说:“种子”吗?

Posted

技术标签:

【中文标题】如何修复 AttributeError:“Image”对象没有“seek”属性。您是说:“种子”吗?【英文标题】:How to fix AttributeError: 'Image' object has no attribute 'seek'. Did you mean: 'seed'? 【发布时间】:2022-01-14 01:27:25 【问题描述】:

我正在尝试使用 python-pptx 创建一个 pptx 或 powerpoint 文件并使用 python wand 库读取图像,但出现类似 AttributeError: 'Image' object has没有属性“寻求”。您是说:“种子”吗?

注意:所有文件都在同一个文件夹中,以“watermarked_”开头

from io import FileIO
import os
from wand.image import Image
from pptx.util import Inches 
from pptx import Presentation 

def create_slide()->FileIO:
    # Creating presentation object
    root = Presentation()
    for file in os.listdir():
        if file.startswith('watermarked_'):
            # Creating slide layout
            first_slide_layout = root.slide_layouts[1] 
            slide = root.slides.add_slide(first_slide_layout)
            shapes = slide.shapes
            
            #Adding title or heading to the slide
            title_shape = shapes.title
            title_shape.text = f" Created By python-pptx for Watermarking "
            
            #Adding sub-title with border to the slide
            body_shape = shapes.placeholders[1]
            tf = body_shape.text_frame
            tf.text = f"This is a watermarked image of file"
                
            with Image(filename = file) as watermarked_image:
                
                #Maintianing the aspect ratio of the image
                width, height = watermarked_image.size
                ratio = height/width
                new_width = width / 2
                new_height = int(new_width * ratio)
                watermarked_image.resize(int(new_width), new_height)
                
                # Add the watermarked image to the slide
                slide.shapes.add_picture(watermarked_image ,Inches(1), Inches(3))
                root.save("Output.pptx")

create_slide()

Traceback (most recent call last):
  File "/Users/quantum/Desktop/image/project.py", line 60, in <module>
quantum@MacBook-Air image % python -u "/Users/quantum/Desktop/image/project.py"
Traceback (most recent call last):
  File "/Users/quantum/Desktop/image/project.py", line 60, in <module>
    create_slide()
  File "/Users/quantum/Desktop/image/project.py", line 57, in create_slide
    slide.shapes.add_picture(watermarked_image ,Inches(1), Inches(3))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/shapes/shapetree.py", line 332, in add_picture
    image_part, rId = self.part.get_or_add_image_part(image_file)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/parts/slide.py", line 39, in get_or_add_image_part
    image_part = self._package.get_or_add_image_part(image_file)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/package.py", line 36, in get_or_add_image_part
    return self._image_parts.get_or_add_image_part(image_file)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/package.py", line 151, in get_or_add_image_part
    image = Image.from_file(image_file)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/parts/image.py", line 168, in from_file
    if callable(getattr(image_file, "seek")):
AttributeError: 'Image' object has no attribute 'seek'. Did you mean: 'seed'?

我们将不胜感激任何经常提供的帮助

【问题讨论】:

缺少 Traceback 并且代码中没有提到 seek。请更新您的问题。 【参考方案1】:

wand.image.Image 对象不是Shapes.add_picture() 的有效参数。该调用的第一个参数需要是图像文件或包含图像的类似文件的对象的 str 路径。

我想这意味着您需要将修改后的图像保存为 JPG 或 PNG 或其他格式,然后提供文件名。您还可以将其保存到 BytesIO 对象并将其传递给 .add_picture(),因为这将被视为类文件对象并且不需要使用文件系统。

【讨论】:

谢谢,我终于可以通过添加 buf = io.BytesIO() watermarked_image.save(buf ) buf.seek(0) # 将水印图像添​​加到幻灯片 slide.shapes .add_picture(buf, 英寸(1), 英寸(1)) 很高兴你得到它的工作@MdTausif,不要忽略通过单击答案左侧的复选标记来接受可以解决你问题的答案。这就是您在 SO 上说“谢谢”的方式,它也可以建立您的声誉点。

以上是关于如何修复 AttributeError:“Image”对象没有“seek”属性。您是说:“种子”吗?的主要内容,如果未能解决你的问题,请参考以下文章

如何修复“AttributeError:模块'tensorflow'没有属性'get_default_graph'”?

如何修复 AttributeError:“系列”对象没有“查找”属性?

如何修复python2.7中的“AttributeError:'module'对象没有属性'storage'”错误

如何修复此 AttributeError:“SubRequest”对象没有属性“getfuncargvalue”?

如何修复 AttributeError:“列表”对象没有属性“编码”

如何修复 AttributeError:“Image”对象没有“seek”属性。您是说:“种子”吗?