How to Add Images to PowerPoint Slides in Python
This guide shows how to embed images in PowerPoint slides using Aspose.Slides FOSS for Python. Call prs.images.add_image() to add image bytes to the presentation’s image collection, then call slide.shapes.add_picture_frame() to place the image on the slide as a picture frame.
Prerequisites
Install the library in your Python environment by running the following pip command before running any examples:
pip install aspose-slides-fossAdd an Image from a File
Read the image bytes and pass them to prs.images.add_image(), then create the picture frame with slide.shapes.add_picture_frame():
from aspose.slides_foss import ShapeType, PictureFillMode
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
# Load image into the presentation's image collection
with open("photo.jpg", "rb") as f:
img = prs.images.add_image(f.read())
# Add a picture frame at (x=50, y=50, width=400, height=300) in points
frame = slide.shapes.add_picture_frame(
ShapeType.RECTANGLE,
50, 50, 400, 300,
img,
)
prs.save("with-image.pptx", SaveFormat.PPTX)The add_picture_frame signature:
add_picture_frame(shape_type, x, y, width, height, image) → PictureFrameAll dimensions are in points (1 point = 1/72 inch). For a standard 16:9 widescreen slide the coordinate space is 960 × 540 points.
Add an Image from Bytes
If you already have the image as bytes (e.g., downloaded from a URL or read from a database), pass them directly to prs.images.add_image(image_bytes):
import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat
image_bytes = open("logo.png", "rb").read() # or any bytes source
with slides.Presentation() as prs:
img = prs.images.add_image(image_bytes)
prs.slides[0].shapes.add_picture_frame(
ShapeType.RECTANGLE,
200, 100, 300, 200,
img,
)
prs.save("logo-slide.pptx", SaveFormat.PPTX)Control the Fill Mode
The picture_fill_format on a PictureFrame controls how the image fills the frame bounds:
from aspose.slides_foss import ShapeType, PictureFillMode
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
img = prs.images.add_image(open("texture.png", "rb").read())
frame = prs.slides[0].shapes.add_picture_frame(
ShapeType.RECTANGLE, 50, 50, 600, 350, img
)
# STRETCH: scale image to fill the frame exactly (default)
frame.picture_format.picture_fill_mode = PictureFillMode.STRETCH
# TILE: repeat the image in a grid pattern
# frame.picture_format.picture_fill_mode = PictureFillMode.TILE
prs.save("filled.pptx", SaveFormat.PPTX)PictureFillMode | Behaviour |
|---|---|
STRETCH | Scale the image to fill the frame, ignoring aspect ratio |
TILE | Repeat the image as a tiled pattern |
Add Multiple Images to Different Slides
Add slides as needed with prs.slides.add_empty_slide(layout) then call prs.images.add_image() and slide.shapes.add_picture_frame() for each image file:
import os
from aspose.slides_foss import ShapeType
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat
image_files = ["slide1.jpg", "slide2.jpg", "slide3.jpg"]
with slides.Presentation() as prs:
layout = prs.slides[0].layout_slide
# Ensure enough slides exist
while len(prs.slides) < len(image_files):
prs.slides.add_empty_slide(layout)
for i, path in enumerate(image_files):
if not os.path.exists(path):
continue
img = prs.images.add_image(open(path, "rb").read())
prs.slides[i].shapes.add_picture_frame(
ShapeType.RECTANGLE, 0, 0, 960, 540, img
)
prs.save("multi-image.pptx", SaveFormat.PPTX)Count Images in an Existing Presentation
Load the file and check len(prs.images) to count images stored in the shared image collection:
import aspose.slides_foss as slides
with slides.Presentation("with-image.pptx") as prs:
print(f"Presentation contains {len(prs.images)} image(s)")The prs.images collection is shared across all slides: the same image bytes are stored once even if the picture frame appears on multiple slides.