Optimize Image Management in PowerPoint with Python
Overview
Images make presentations more engaging and interesting. In Microsoft PowerPoint, you can insert pictures from a file, the internet, or other sources onto slides. Similarly, Aspose.Slides lets you add images to slides in several ways.
Tip
Aspose provides free converters—JPEG to PowerPoint and PNG to PowerPoint—that let you quickly create presentations from images.Info
If you want to add an image as a frame object—especially if you plan to use standard formatting options such as resizing or applying effects—see Add Picture Frames to Presentations with Python.Note
You can use image and presentation I/O operations to convert images between formats. See these pages: convert image to JPG; convert JPG to image; convert JPG to PNG; convert PNG to JPG; convert PNG to SVG; and convert SVG to PNG.Aspose.Slides supports working with images in popular formats such as JPEG, PNG, BMP, GIF, and others.
Add Images Stored Locally to Slides
You can add one or more images from your computer to a slide in a presentation. The following Python example shows how to add an image to a slide:
import aspose.slides as slides
with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    with open("image.jpeg", "rb") as image_stream:
        image = presentation.images.add_image(image_stream)
        slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
    presentation.save("presentation_with_image.pptx", slides.export.SaveFormat.PPTX)
Add Images From the Web to Slides
If the image you want to add to a slide isn’t available on your computer, you can insert it directly from the web.
The following Python example shows how to add an image from a URL to a slide:
import aspose.slides as slides
import urllib2
import base64
with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    image_data = base64.b64encode(urllib2.urlopen("[REPLACE WITH URL]").read())
    image = presentation.images.add_image(image_data)
    slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
    
    presentation.save("presentation.pptx", slides.export.SaveFormat.PPTX)
Add Images to Slide Masters
A slide master is the top-level slide that stores and controls information—theme, layout, and so on—for all slides beneath it. When you add an image to a slide master, that image appears on every slide that uses that master.
The following Python example shows how to add an image to a slide master:
import aspose.slides as slides
with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    master_slide = slide.layout_slide.master_slide
    with open("image.jpeg", "rb") as image_stream:
        image = presentation.images.add_image(image_stream)
        master_slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
    presentation.save("master_with_image.pptx", slides.export.SaveFormat.PPTX)
Set an Image as a Slide Background
You may want to use an image as the background for a specific slide or multiple slides. For details, see Set an Image as the Background for a Slide.
Add SVG to Presentations
You can insert any image into a presentation using the add_picture_frame method of the ShapeCollection class.
To create an image object from an SVG, follow these steps:
- Create an SvgImage and add it to the presentation’s image collection.
- Create PPImage object from SvgImage.
- Create PictureFrame object using PPImage.
The following Python sample shows how to add an SVG image to a presentation using these steps:
import aspose.slides as slides
with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    # Read the content of an SVG file.
    with open("sample.svg", "rt") as image_stream:
        svg_content = image_stream.read()
        # Create an SvgImage object.
        svg_image = slides.SvgImage(svg_content)
        # Create a PPImage object.
        pp_image = presentation.images.add_image(svg_image)
        # Create a new PictureFrame.
        slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 200, 100, pp_image.width, pp_image.height, pp_image)
        # Save the presentation in PPTX format.
        presentation.save("presentation_with_SVG.pptx", slides.export.SaveFormat.PPTX)
Convert SVG to a Set of Shapes
Aspose.Slides converts SVGs into a set of shapes in a way similar to PowerPoint’s SVG handling.

This functionality is provided by an overload of the add_group_shape method in the ShapeCollection class that takes an SvgImage as its first argument.
The sample code below shows how to convert an SVG file into a set of shapes.
import aspose.slides as slides
with slides.Presentation() as presentation:
    # Read the SVG file content.
    with open("sample.svg","rt") as image_stream:
        svg_content = image_stream.read()
        # Create an SvgImage object.
        svg_image = slides.SvgImage(svg_content)
        # Get the slide size.
        slide_size = presentation.slide_size.size
        # Convert the SVG image into a group of shapes and scale it to the slide size.
        presentation.slides[0].shapes.add_group_shape(svg_image, 0, 0, slide_size.width, slide_size.height)
        # Save the presentation in PPTX format.
        presentation.save("shapes_from_SVG.pptx", slides.export.SaveFormat.PPTX)
Add Images as EMF in Slides
Aspose.Slides for Python lets you insert Enhanced Metafile (EMF) images into presentations.
The following Python example demonstrates this:
with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    with open("image.emf", "rb") as image_stream:
        emf_image = presentation.images.add_image(image_stream)
        slide_size = presentation.slide_size.size
        slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 0, 0, slide_size.width, slide_size.height, emf_image)
    
    presentation.save("presentation_with_EMF.pptx", slides.export.SaveFormat.PPTX)
Replace Images in the Image Collection
Aspose.Slides allows you to replace images stored in a presentation’s image collection, including those used by slide shapes. This section outlines several approaches to updating images in the collection. The API provides straightforward methods to replace an image with raw byte data, an IImage instance, or another image that already exists in the collection.
Follow these steps:
- Load the presentation that contains the images using the Presentation class.
- Load a new image from a file into a byte array.
- Replace the target image with the new image using the byte array.
- Alternatively, load the image into an IImage object and replace the target image with that object.
- Or replace the target image with an image that already exists in the presentation’s image collection.
- Save the modified presentation as a PPTX file.
def read_all_bytes(file_name):
    with open(file_name, "rb") as stream:
        return stream.read()
# Instantiate the Presentation class that represents a presentation file.
with slides.Presentation("sample.pptx") as presentation:
    # The first way.
    image_data = read_all_bytes("image0.jpeg")
    old_image = presentation.images[0]
    old_image.replace_image(image_data)
    # The second way.
    new_image = slides.Images.from_file("image1.jpeg")
    old_image = presentation.images[1]
    old_image.replace_image(new_image)
    # The third way.
    old_image = presentation.images[2]
    old_image.replace_image(presentation.images[3])
    # Save the presentation to a file.
    presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Info
With Aspose’s free Text to GIF converter, you can easily animate text and create GIFs from text.FAQ
Does the original image resolution remain intact after insertion?
Yes. The source pixels are preserved, but the final appearance depends on how the picture is scaled on the slide and any compression applied on save.
What’s the best way to replace the same logo across dozens of slides at once?
Place the logo on the master slide or a layout and replace it in the presentation’s image collection—updates will propagate to all elements that use that resource.
Can an inserted SVG be converted into editable shapes?
Yes. You can convert an SVG into a group of shapes, after which individual parts become editable with standard shape properties.
How can I set a picture as the background for multiple slides at once?
Assign the image as the background on the master slide or the relevant layout—any slides using that master/layout will inherit the background.
How do I prevent the presentation from “ballooning” in size because of many pictures?
Reuse a single image resource instead of duplicates, choose reasonable resolutions, apply compression on save, and keep repeated graphics on the master where appropriate.