Picture Frame

A picture frame is a shape that contains an image—it is like a picture in a frame.

You can add an image to a slide through a picture frame. This way, you get to format the image by formatting the picture frame.

Create Picture Frame

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
  4. Specify the image’s width and height.
  5. Create a PictureFrame based on the image’s width and height through the AddPictureFrame method exposed by the shape object associated with the referenced slide.
  6. Add a picture frame (containing the picture) to the slide.
  7. Write the modified presentation as a PPTX file.

This Python code shows you how to create a picture frame:

import aspose.slides as slides
import aspose.pydrawing as draw

# Instantiates the Presentation class that represents a PPTX file
with slides.Presentation() as pres:
    # Gets the first slide
    sld = pres.slides[0]

    # Instantiates the ImageEx class
    with open("img.jpeg", "rb") as in_file:
        image = pres.images.add_image(in_file)

        # Adds a frame with the picture's equivalent height and width
        pf = sld.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 50, 150, image.width, image.height, image)

        # Applies some formatting to the PictureFrameEx
        pf.line_format.fill_format.fill_type = slides.FillType.SOLID
        pf.line_format.fill_format.solid_fill_color.color = draw.Color.blue
        pf.line_format.width = 20
        pf.rotation = 45

        # Writes the PPTX file to disk
        pres.save("RectPicFrameFormat_out.pptx", slides.export.SaveFormat.PPTX)

Create Picture Frame with Relative Scale

By altering an image’s relative scaling, you can create a more complicated picture frame.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add an image to the presentation image collection.
  4. Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
  5. Specify the image’s relative width and height in the picture frame.
  6. Write the modified presentation as a PPTX file.

This Python code shows you how to create a picture frame with relative scale:

import aspose.slides as slides

# Instantiates the Presentation class that represents a PPTX file
with slides.Presentation() as presentation:
    # Loads the Image that will be added to the presentaiton image collection
    with open("img.jpeg", "rb") as in_file:
        image = presentation.images.add_image(in_file)

        # Adds a picture frame to the slide
        pf = presentation.slides[0].shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 50, 50, 100, 100, image)

        # Sets the relative scale width and height
        pf.relative_scale_height = 0.8
        pf.relative_scale_width = 1.35

        # Saves the presentation
        presentation.save("Adding Picture Frame with Relative Scale_out.pptx", slides.export.SaveFormat.PPTX)

Extract Image from Picture Frame

You can extract images from PictureFrame objects and save them in PNG, JPG, and other formats. The code example below demonstrates how to extract an image from the document “sample.pptx” and save it in PNG format.

import aspose.slides as slides

with slides.Presentation("sample.pptx") as presentation:
    first_slide = presentation.slides[0]
    first_shape = first_slide.shapes[0]

    if isinstance(first_shape, slides.PictureFrame):
        image = first_shape.picture_format.picture.image.image
        image.save("slide_1_shape_1.png", slides.ImageFormat.PNG)

Get Transparency of Image

Aspose.Slides allows you to get the transparency effect applied to an image. This Python code demonstrates the operation:

import aspose.slides as slides

with slides.Presentation("pres.pptx") as presentation:
    pictureFrame = presentation.slides[0].shapes[0]
    imageTransform = pictureFrame.picture_format.picture.image_transform
    for effect in imageTransform:
        if type(effect) is slides.AlphaModulateFixed:
            transparencyValue = 100 - effect.amount
            print("Picture transparency: " + str(transparencyValue))

Picture Frame Formatting

Aspose.Slides provides many formatting options that can be applied to a picture frame. Using those options, you can alter a picture frame to make it match specific requirements.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Create an IPPImage object by adding an image to the IImagescollection associated with the presentation object that will be used to fill the shape.
  4. Specify the image’s width and height.
  5. Create a PictureFrame based on the image’s width and height through the AddPictureFrame method exposed by the IShapes object associated with the referenced slide.
  6. Add the picture frame (containing the picture) to the slide.
  7. Set the picture frame’s line color.
  8. Set the picture frame’s line width.
  9. Rotate the picture frame by giving it either a positive or negative value.
    • A positive value rotates the image clockwise.
    • A negative value rotates the image anti-clockwise.
  10. Add the picture frame (containing the picture) to the slide.
  11. Write the modified presentation as a PPTX file.

This Python code demonstrates the picture frame formatting process:

import aspose.slides as slides
import aspose.pydrawing as draw

# Instantiate the Presentation class that represents a PPTX file
with slides.Presentation() as pres:
    # Gets the first slide
    sld = pres.slides[0]

    with open("img.jpeg", "rb") as in_file:
        imgx = pres.images.add_image(in_file)

         # Adds a picture frame with the picture's equivalent height and width
        pf = sld.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 50, 150, imgx.width, imgx.height, imgx)

        # Applies some formatting to PictureFrameEx
        pf.line_format.fill_format.fill_type = slides.FillType.SOLID
        pf.line_format.fill_format.solid_fill_color.color = draw.Color.blue
        pf.line_format.width = 20
        pf.rotation = 45

    # Writes the PPTX file to disk
    pres.save("RectPicFrameFormat_out.pptx", slides.export.SaveFormat.PPTX)

To avoid large presentation sizes, you can add images (or videos) through links instead of embedding the files directly into presentations. This Python code shows you how to add an image and video into a placeholder:

import aspose.slides as slides

with slides.Presentation("input.pptx") as presentation:
    shapesToRemove = []

    for autoShape in presentation.slides[0].shapes:
        if autoShape.placeholder is None:
            continue
        
        if autoShape.placeholder.type == slides.PlaceholderType.PICTURE:
            pictureFrame = presentation.slides[0].shapes.add_picture_frame(slides.ShapeType.RECTANGLE,
                    autoShape.x, autoShape.y, autoShape.width, autoShape.height, None)

            pictureFrame.picture_format.picture.link_path_long = \
                "https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg"

            shapesToRemove.append(autoShape)

        elif autoShape.placeholder.type == slides.PlaceholderType.MEDIA:
            videoFrame = presentation.slides[0].shapes.add_video_frame(
                autoShape.X, autoShape.Y, autoShape.width, autoShape.height, "")

            videoFrame.picture_format.picture.link_path_long = \
                "https://upload.wikimedia.org/wikipedia/commons/3/3a/I.M_at_Old_School_Public_Broadcasting_in_October_2016_02.jpg"

            videoFrame.link_path_long = "https://youtu.be/t_1LYZ102RA"
            shapesToRemove.append(autoShape)
        
    

    for shape in shapesToRemove:
        presentation.slides[0].shapes.remove(shape)

    presentation.save("output.pptx", slides.export.SaveFormat.PPTX)

Crop Image

This Python code shows you how to crop an existing image on a slide:

import aspose.slides as slides

with slides.Presentation() as presentation:
    # Creates new image object
    newImage = presentation.images.add_image(slides.Images.from_file(imagePath))

    # Adds a PictureFrame to a Slide
    picFrame = presentation.slides[0].shapes.add_picture_frame(
        slides.ShapeType.RECTANGLE, 100, 100, 420, 250, newImage)

    # Crops the image (percentage values)
    picFrame.picture_format.crop_left = 23.6
    picFrame.picture_format.crop_right = 21.5
    picFrame.picture_format.crop_top = 3
    picFrame.picture_format.crop_bottom = 31

    # Saves the result
    presentation.save(outPptxFile, slides.export.SaveFormat.PPTX)

Delete Cropped Areas of Picture

If you want to delete the cropped areas of an image contained in a frame, you can use the delete_picture_cropped_areas method. This method returns the cropped image or the origin image if cropping is unnecessary.

This Python code demonstrates the operation:

import aspose.slides as slides

with slides.Presentation(path + "PictureFrameCrop.pptx") as pres:
    slide = pres.slides[0]

    # Gets the PictureFrame from the first slide
    picture_frame = slides.shape[0]

    # Deletes cropped areas of the PictureFrame image and returns the cropped image
    cropped_image = picture_frame.picture_format.delete_picture_cropped_areas()

    # Saves the result
    pres.save(path + "PictureFrameDeleteCroppedAreas.pptx", slides.export.SaveFormat.PPTX)

Lock Aspect Ratio

If you want a shape containing an image to retain its aspect ratio even after you change the image dimensions, you can use the aspect_ratio_locked property to set the Lock Aspect Ratio setting.

This Python code shows you how to lock a shape’s aspect ratio:

from aspose.slides import SlideLayoutType, Presentation, ShapeType
from aspose.pydrawing import Image

with Presentation("pres.pptx") as pres:
    layout = pres.layout_slides.get_by_type(SlideLayoutType.CUSTOM)
    emptySlide = pres.slides.add_empty_slide(layout)
    image = Image.from_file("image.png")
    presImage = pres.images.add_image(image)

    pictureFrame = emptySlide.shapes.add_picture_frame(ShapeType.RECTANGLE, 50, 150, presImage.width, presImage.height, presImage)

    # set shape to have to preserve aspect ratio on resizing
    pictureFrame.picture_frame_lock.aspect_ratio_locked = True

Use StretchOff Property

Using the StretchOffsetLeft, StretchOffsetTop, StretchOffsetRight and StretchOffsetBottom properties from the IPictureFillFormat interface and PictureFillFormat class, you can specify a fill rectangle.

When stretching is specified for an image, a source rectangle is scaled to fit the specified fill rectangle. Each edge of the fill rectangle is defined by a percentage offset from the corresponding edge of the shape’s bounding box. A positive percentage specifies an inset while a negative percentage specifies an outset.

  1. Create an instance of the Presentation class.
  2. Get a slide’s reference through its index.
  3. Add a rectangle AutoShape.
  4. Create an image.
  5. Set the shape’s fill type.
  6. Set the shape’s picture fill mode.
  7. Add a set image to fill the shape.
  8. Specify image offsets from the corresponding edge of the shape’s bounding box
  9. Write the modified presentation as a PPTX file.

This Python code demonstrates a process in which a StretchOff property is used:

import aspose.slides as slides

# Instantiates the Prseetation class that represents a PPTX file
with slides.Presentation() as pres:

    # Gets the first slide
    slide = pres.slides[0]

    # Instantiates the ImageEx class
    with open("img.jpeg", "rb") as in_file:
        imgx = pres.images.add_image(in_file)

        # Adds a picture frame with the picture's equivalent height and width
        shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 300, 300)

        # Sets the shape's fill type
        shape.fill_format.fill_type = slides.FillType.PICTURE

        # Sets the shape's picture fill mode
        shape.fill_format.picture_fill_format.picture_fill_mode = slides.PictureFillMode.STRETCH

        # Sets the image to fill the shape
        shape.fill_format.picture_fill_format.picture.image = imgx

        # Specifies image offsets from the corresponding edge of the shape's bounding box
        shape.fill_format.picture_fill_format.stretch_offset_left = 25
        shape.fill_format.picture_fill_format.stretch_offset_right = 25
        shape.fill_format.picture_fill_format.stretch_offset_top = -20
        shape.fill_format.picture_fill_format.stretch_offset_bottom = -10
    
    # Writes the PPTX file to disk
    pres.save("StretchOffsetLeftForPictureFrame_out.pptx", slides.export.SaveFormat.PPTX)