Image

Images in Slides In Presentations

Images make presentations more engaging and interesting. In Microsoft PowerPoint, you can insert pictures from a file, the internet, or other locations onto slides. Similarly, Aspose.Slides allows you to add images to slides in your presentations through different procedures.

Aspose.Slides supports operations with images in these popular formats: JPEG, PNG, BMP, GIF, and others.

Adding Images Stored Locally to Slides

You can add one or several images on your computer onto a slide in a presentation. This sample code in Python shows you how to add an image to a slide:

import aspose.slides as slides

with slides.Presentation() as pres:
    slide = pres.slides[0]
    with open("img.jpeg", "rb") as in_file:
        image = pres.images.add_image(in_file)
        slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
    
    pres.save("pres_with_image.pptx", slides.export.SaveFormat.PPTX)

Adding Images From the Web to Slides

If the image you want to add to a slide is unavailable on your computer, you can add the image directly from the web.

This sample code shows you how to add an image from the web to a slide in Python:

import aspose.slides as slides
import urllib2
import base64

with slides.Presentation() as pres:
    slide = pres.slides[0]
    imageData = base64.b64encode(urllib2.urlopen("[REPLACE WITH URL]").read())

    image = pres.images.add_image(imageData)
    slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
    
    pres.save("pres.pptx", slides.export.SaveFormat.PPTX)

Adding Images to Slide Masters

A slide master is the top slide that stores and controls information (theme, layout, etc.) about all slides under it. So, when you add an image to a slide master, that image appears on every slide under that slide master.

This Python sample code shows you how to add an image to a slide master:

import aspose.slides as slides

with slides.Presentation() as pres:
    slide = pres.slides[0]
    masterSlide = slide.layout_slide.master_slide
    with open("img.jpeg", "rb") as in_file:
        image = pres.images.add_image(in_file)
        masterSlide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
        
    pres.save("master_with_image.pptx", slides.export.SaveFormat.PPTX)

Adding Images as Slide Background

You may decide to use a picture as the background for a specific slide or several slides. In that case, you have to see Setting Images as Backgrounds for Slides.

Adding SVG to Presentations

You can add or insert any image into a presentation by using the add_picture_frame method that belongs to the IShapeCollection interface.

To create an image object based on SVG image, you can do it this way:

  1. Create SvgImage object to insert it to ImageShapeCollection
  2. Create PPImage object from ISvgImage
  3. Create PictureFrame object using IPPImage interface

This sample code shows you how to implement the steps above to add an SVG image into a presentation:

import aspose.slides as slides

# Create new presentation
with slides.Presentation() as p:
    # Read SVG file content
    with open("sample.svg","rt") as in_file:
        svgContent = in_file.read()
        # Create SvgImage object
        svgImage = slides.SvgImage(svgContent)

        # Create PPImage object
        ppImage = p.images.add_image(svgImage)

        # Creates a new PictureFrame 
        p.slides[0].shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 200, 100, ppImage.width, ppImage.height, ppImage)

        # Save presentation in PPTX format
        p.save("presentation_with-svg.pptx", slides.export.SaveFormat.PPTX)

Converting SVG to a Set of Shapes

Aspose.Slides' conversion of SVG to a set of shapes is similar to the PowerPoint functionality used to work with SVG images:

PowerPoint Popup Menu

The functionality is provided by one of the overloads of the add_group_shape method of the IShapeCollection interface that takes an ISvgImage object as the first argument.

This sample code shows you how to use the described method to convert an SVG file to a set of shapes:

import aspose.slides as slides

with slides.Presentation() as presentation:
    # Read SVG file content
    with open("sample.svg","rt") as in_file:
        svgContent = in_file.read()
        # Create SvgImage object
        svgImage = slides.SvgImage(svgContent)

        # Get slide size
        slide_size = presentation.slide_size.size

        # Convert SVG image to group of shapes scaling it to slide size
        presentation.slides[0].shapes.add_group_shape(svgImage, 0, 0, slide_size.width, slide_size.height)

        # Save presentation in PPTX format
        presentation.save("presentation_with_shape_svg.pptx", slides.export.SaveFormat.PPTX)

Adding Images as EMF in Slides

Aspose.Slides for Python via .NET allows you to add the EMF image. 

This sample code shows you how to perform the described task:

with slides.Presentation() as pres:
    slide = pres.slides[0]
    with open("image.emf", "rb") as in_file:
        emfImage = pres.images.add_image(in_file)
        slide_size = pres.slide_size.size
        slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 0, 0, slide_size.width, slide_size.height, emfImage)
    
    pres.save("pres_with_emf.pptx", slides.export.SaveFormat.PPTX)