在使用 python-pptx 创建演示文稿并插入图片时,如何获取图片占位符的尺寸来调整图像大小?

Rob*_*ite 4 python image-processing python-pptx

我正在尝试使用 python-pptx 从模板插入一张调整大小以适合图片占位符尺寸的图片。从我在文档中找到的内容来看,我不认为 API 可以直接访问此内容。有什么建议我可以使用图书馆或其他方式来做到这一点吗?

我有一个正在运行的代码,它将插入一系列图像到一组模板幻灯片中,以使用 Powerpoint 自动创建报告。

这是执行大部分相关工作的函数。应用程序的其他部分正在创建演示文稿并插入幻灯片等。

def insert_images(slide, slide_num, images_path, image_df):

    """
    Insert images into a slide.
    :param slide: = slide object from Presentation class 
    :param slide_num: the template slide number for formatting
    :param images_path: the directory to the folder with all the images
    :param image_df: Pandas data frame regarding information of each image in images_path
    :return: None
    """

    placeholders = get_image_placeholders(slide)
    #print(placeholders)
    image_pool = image_df[image_df['slide_num'] == slide_num]

    try:
        assert len(placeholders) == len(image_pool.index)
    except AssertionError:
        print('Length of placeholders in slide does not match image naming.')
    i = 0
    for idx, image in image_pool.iterrows():
        #print(image)
        image_path = os.path.join(images_path, image.path)
        pic = slide.placeholders[placeholders[i]].insert_picture(image_path)
        #print(image.path)
        # TODO: Add resize - get dimensions of pic placeholder
        line = pic.line
        print(image['view'])
        if image['view'] == 'red':
            line.color.rgb = RGBColor(255, 0, 0)
        elif image['view'] == 'green':
            line.color.rgb = RGBColor(0, 255, 0)
        elif image['view'] == 'blue':
            line.color.rgb = RGBColor(0, 0, 255)
        else:
            line.color.rgb = RGBColor(0, 0, 0)
        line.width = Pt(2.25)
        i+=1
Run Code Online (Sandbox Code Playgroud)

问题是,当我将图片插入图片占位符时,图像会被裁剪,而不是重新调整大小。我不希望用户知道硬编码到我的脚本中的尺寸。如果使用的图像相对较大,则可能会裁剪掉很大一部分而无法使用。

sca*_*nny 7

返回的图片对象与其PicturePlaceholder.insert_picture()派生的占位符具有相同的位置和大小。它被裁剪以完全填充该空间。顶部和底部或左侧和右侧会被裁剪,具体取决于占位符和您插入的图像的相对纵横比。这与将图片插入图片占位符时 PowerPoint 表现出的行为相同。

如果要删除裁剪,只需将所有裁剪值设置为 0:

picture = placeholder.insert_picture(...)
picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0
Run Code Online (Sandbox Code Playgroud)

这不会改变(左上角)的位置,但几乎总是会改变尺寸,使其变得更宽或更高(但不会两者兼而有之)。

因此,这很容易解决第一个问题,但当然也会给您带来第二个问题,即如何将图片放置在您想要的位置以及如何在不改变纵横比(拉伸或挤压它)的情况下适当缩放它。

这在很大程度上取决于您想要实现的目标以及您最满意的结果。这就是为什么它不是自动的;这是无法预测的。

您可以像这样找到图像的“原始”宽度和高度:

width, height = picture.image.size  # ---width and height are int pixel-counts
Run Code Online (Sandbox Code Playgroud)

从那里,您需要比较原始占位符和插入的图像的纵横比,并调整图片形状的宽度或高度。

假设您想保持相同的位置,但将占位符的宽度和高度保持为各自的最大值,以便整个图片适合空间,但在底部或右侧有一个“边距”:

available_width = picture.width
available_height = picture.height
image_width, image_height = picture.image.size
placeholder_aspect_ratio = float(available_width) / float(available_height)
image_aspect_ratio = float(image_width) / float(image_height)

# Get initial image placeholder left and top positions
pos_left, pos_top = picture.left, picture.top

picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0

# ---if the placeholder is "wider" in aspect, shrink the picture width while
# ---maintaining the image aspect ratio
if placeholder_aspect_ratio > image_aspect_ratio:
    picture.width = int(image_aspect_ratio * available_height)
    picture.height = available_height

# ---otherwise shrink the height
else:
    picture.height = int(available_width/image_aspect_ratio)
    picture.width = available_width

# Set the picture left and top position to the initial placeholder one
picture.left, picture.top = pos_left, pos_top

# Or if we want to center it vertically:
# picture.top = picture.top + int(picture.height/2)
Run Code Online (Sandbox Code Playgroud)

这可以详细说明为将图像“居中”在原始空间内,并且可能使用“负裁剪”来保留原始占位符大小。

我还没有对此进行测试,您可能需要进行一些调整,但希望这能让您了解如何继续。这将是一件好事,可以提取到它自己的函数中,例如adjust_picture_to_fit(picture).

  • 这工作得相当好,谢谢,这绝对回答了我关于获取图像大小和占位符大小的问题。我确实必须分别将 picture.height = available_height 和 picture.width = available_width 添加到 if 和 else 分支中。它将它们分配给 0。然而,这似乎也删除了这些位置。所有图像都放置在幻灯片的左上角,而不是指定的位置。因此,我首先在进行任何更改之前抓取位置,然后使用 pos_left, pos_top = picture.left, picture.top 重新分配它们 (5认同)
  • 您好,我尝试了这个解决方案,但是设置“picture.width”或“picture.height”会使图片在powerpoint中消失。 (5认同)