ImageCollection / PPImage

ImageCollection and PPImage — Aspose.Slides FOSS Python API Reference

Images in a presentation are stored in the ppt/media/ part of the .pptx package. The ImageCollection (accessed via prs.images) manages them. Each stored image is represented by a PPImage. Shapes that display images use a PictureFrame and a PictureFillFormat.

Package: aspose.slides_foss


PPImage

PPImage represents a single embedded image in the presentation package.

PPImage Properties

PropertyTypeAccessDescription
content_typestrReadMIME type of the image (e.g. "image/png", "image/jpeg").
widthintReadImage width in pixels.
heightintReadImage height in pixels.
xintReadX-offset (always 0).
yintReadY-offset (always 0).
binary_datalist[int]ReadA copy of the raw image bytes as a list of integers.
imageImageReadAn Image wrapper around the raw bytes.

PPImage Methods

MethodSignatureDescription
replace_image(new_image) -> NoneReplace the stored image with new bytes. Accepts bytes, bytearray, list[int], a PPImage, or an Image object.

ImageCollection

ImageCollection (accessed via prs.images) holds all PPImage objects embedded in the presentation.

ImageCollection Methods

MethodSignatureDescription
add_image(source) -> PPImageAdd an image from bytes, bytearray, list[int], a file-like stream, an existing PPImage, or an Image object. Returns the existing PPImage if the same data is already embedded.
__getitem__[index: int] -> PPImageReturn the image at zero-based index.
__len__() -> intTotal number of embedded images.
__iter__N/AIterate over all images.

Images (static factory)

Images provides static helpers for creating Image objects from disk or streams. The resulting Image can then be passed to prs.images.add_image().

Images Static Methods

MethodSignatureDescription
Images.from_file(filename: str) -> ImageLoad image bytes from a file path.
Images.from_stream(stream) -> ImageLoad image bytes from a binary stream.

PictureFillFormat

PictureFillFormat controls how an image fills a shape (stretch or tile). Access it via picture_frame.picture_format or via shape.fill_format.picture_fill_format when fill_type == FillType.PICTURE.

PictureFillFormat Properties

PropertyTypeAccessDescription
picture_fill_modePictureFillModeRead/WriteSTRETCH (default) or TILE.
pictureSlidesPictureReadThe embedded picture reference.
dpiintRead/WriteDPI used for filling. Default -1 (unset).
crop_leftfloatRead/WritePercentage cropped from the left edge.
crop_topfloatRead/WritePercentage cropped from the top edge.
crop_rightfloatRead/WritePercentage cropped from the right edge.
crop_bottomfloatRead/WritePercentage cropped from the bottom edge.
stretch_offset_leftfloatRead/WriteLeft inset/outset when stretching.
stretch_offset_topfloatRead/WriteTop inset/outset when stretching.
stretch_offset_rightfloatRead/WriteRight inset/outset when stretching.
stretch_offset_bottomfloatRead/WriteBottom inset/outset when stretching.
tile_offset_xfloatRead/WriteHorizontal tile offset in points (tile mode).
tile_offset_yfloatRead/WriteVertical tile offset in points (tile mode).
tile_scale_xfloatRead/WriteHorizontal scale percentage for tile mode.
tile_scale_yfloatRead/WriteVertical scale percentage for tile mode.
tile_alignmentRectangleAlignmentRead/WriteAlignment anchor for tile mode.
tile_flipTileFlipRead/WriteFlip mode: NO_FLIP, FLIP_X, FLIP_Y, FLIP_BOTH.

PictureFrame

PictureFrame is the shape that displays an embedded image. Add one via slide.shapes.add_picture_frame().

PictureFrame Properties

PropertyTypeAccessDescription
picture_formatPictureFillFormatReadFill format for the embedded image.
shape_typeShapeTypeRead/WriteShape outline type (defaults to RECTANGLE).
picture_frame_lockPictureFrameLockReadLock settings for this picture frame.
relative_scale_heightfloatRead/WriteHeight scale relative to original image size. 1.0 = 100%.
relative_scale_widthfloatRead/WriteWidth scale relative to original image size. 1.0 = 100%.
is_cameoboolReadAlways False in this implementation.

PictureFrame also inherits the standard shape properties (x, y, width, height, name, fill_format, line_format, effect_format) from GeometryShape.


Code Examples

Add an Image to a Slide

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    with open("logo.png", "rb") as f:
        image_data = f.read()

    pp_img = prs.images.add_image(image_data)
    frame = slide.shapes.add_picture_frame(
        ShapeType.RECTANGLE, 50, 50, 300, 200, pp_img
    )
    prs.save("with-image.pptx", SaveFormat.PPTX)

Load Image Using Images Factory

import aspose.slides_foss as slides
from aspose.slides_foss import Images, ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    img = Images.from_file("photo.jpg")
    pp_img = prs.images.add_image(img)
    slide.shapes.add_picture_frame(ShapeType.RECTANGLE, 100, 100, 400, 300, pp_img)

    prs.save("photo-slide.pptx", SaveFormat.PPTX)

Crop an Image in a PictureFrame

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation("deck.pptx") as prs:
    slide = prs.slides[0]

    with open("banner.png", "rb") as f:
        pp_img = prs.images.add_image(f.read())

    frame = slide.shapes.add_picture_frame(
        ShapeType.RECTANGLE, 50, 50, 600, 200, pp_img
    )
    # Crop 10% from left and right to focus on centre
    frame.picture_format.crop_left = 10.0
    frame.picture_format.crop_right = 10.0

    prs.save("cropped.pptx", SaveFormat.PPTX)

Iterate Over All Embedded Images

import aspose.slides_foss as slides

with slides.Presentation("deck.pptx") as prs:
    print(f"Total images: {len(prs.images)}")
    for i, img in enumerate(prs.images):
        print(f"  [{i}] {img.content_type}  {img.width}x{img.height}px")

Replace an Embedded Image

import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation("deck.pptx") as prs:
    if len(prs.images) > 0:
        with open("new-logo.png", "rb") as f:
            new_data = f.read()
        prs.images[0].replace_image(new_data)
    prs.save("deck-updated.pptx", SaveFormat.PPTX)

See Also