在创建演示文稿并使用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

问题是,当我将图片插入图片占位符时,图像是裁剪而不是重新尺寸的。我不希望用户知道在脚本中硬代码的尺寸。如果所使用的图像相对较大,则可能会裁剪很大,而只是无法使用。

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)
# 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)

这可以详细说明"中心"原始空间内的图像,也许是为了使用"负面裁剪"。保留原始占位符的大小。

我尚未对此进行测试,您可能需要进行一些调整,但希望这使您有一个想法。提取自己的功能是一件好事,例如adjust_picture_to_fit(picture)

这对我有用。我的图像大于占位符(slide.shapes[2](。

picture = slide.shapes[2].insert_picture(img_path)
picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0