首页 > 解决方案 > 在创建演示文稿并使用 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

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

标签: pythonimage-processingpython-pptx

解决方案


返回的图片对象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

这不会改变(左上角的)位置,但几乎总是会改变大小,使其更宽或更高(但不能同时)。

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

这在很大程度上取决于您要完成的工作以及您最满意的结果。这就是为什么它不是自动的;只是无法预测。

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

width, height = picture.image.size  # ---width and height are int pixel-counts

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

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

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)

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)
# ---otherwise shrink the height
else:
    picture.height = int(available_width/image_aspect_ratio)

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

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


推荐阅读