Skip to content Skip to sidebar Skip to footer

How Can I Get The Dimensions Of A Picture Placeholder To Re-size An Image When Creating A Presentation And Inserting A Picture Using Python-pptx?

I'm trying to insert a picture that is re-sized to fit the dimensions of the picture placeholder from a template using python-pptx. I don't believe the API has direct access to thi

Solution 1:

The picture object returned by PicturePlaceholder.insert_picture() has the same position and size as the placeholder it derives from. It is cropped to completely fill that space. Either the tops and bottoms are cropped or the left and right sides, depending on the relative aspect ratio of the placeholder and the image you insert. This is the same behavior PowerPoint exhibits when you insert a picture into a picture placeholder.

If you want to remove the cropping, simply set all cropping values to 0:

picture = placeholder.insert_picture(...)
picture.crop_top = 0picture.crop_left = 0picture.crop_bottom = 0picture.crop_right = 0

This will not change the position (of the top-left corner) but will almost always change the size, making it either wider or taller (but not both).

So this solves the first problem easily, but of course presents you with a second one, which is how to position the picture where you want it and how to scale it appropriately without changing the aspect ratio (stretching or squeezing it).

This depends a great deal on what you're trying to accomplish and what outcome you find most pleasing. This is why it is not automatic; it's just not possible to predict.

You can find the "native" width and height of the image like this:

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

From there you'll need to compare aspect ratios of the original placeholder and the image you inserted and either adjust the width or height of the picture shape.

So say you wanted to keep the same position but maintain the width and height of the placeholder as respective maximums such that the entire picture fits in the space but has a "margin" either on the bottom or the right:

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 ratioif placeholder_aspect_ratio > image_aspect_ratio:
    picture.width = int(image_aspect_ratio * available_height)
# ---otherwise shrink the heightelse:
    picture.height = int(available_width/image_aspect_ratio)

This could be elaborated to "center" the image within the original space and perhaps to use "negative cropping" to retain the original placeholder size.

I haven't tested this and you might need to make some adjustments, but hopefully this gives you an idea how to proceed. This would be a good thing to extract to its own function, like adjust_picture_to_fit(picture).

Solution 2:

This worked for me. My image is larger than the placeholder (slide.shapes[2]).

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

Post a Comment for "How Can I Get The Dimensions Of A Picture Placeholder To Re-size An Image When Creating A Presentation And Inserting A Picture Using Python-pptx?"