Center images in Google Slides using Apps Script

In this tutorial, I'll show you how to center images in a Google Slides presentation using Apps Script. Consider the following presentation that has three slides in it. Each slide contains an image that is not centered. Our goal is to use Apps Script to center these images.

Slide 1

Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is not centered.

Slide 2

Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is not centered.

Slide 3

Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is not centered.

Here is the code to center images in a Google Slides presentation using Google Apps Script.

function centerAllImagesInSlide() {
 // Load the presentation
 var presentation = SlidesApp.openById("<SLIDES_ID>");

 // Get the width and height of the presentation
 var pageWidth = presentation.getPageWidth();
 var pageHeight = presentation.getPageHeight();
  // Load the slides in the presentation
 var slides = presentation.getSlides();

 // For each slide, center the images found on the slide
 // Note: if a slide contains more than a single image, they will
 // overlap when you center them.
 slides.forEach(function(slide) {
   var images = slide.getImages();
   images.forEach(function(image) {
     image.setLeft(pageWidth/2 -image.getWidth()/2);
     image.setTop(pageHeight/2 - image.getHeight()/2);
   });
 });
}

When you run the centerAllImagesInSlide() function, the images in your Google Slides presentation will be centered.

Before

After

Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is not centered. Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is centered.
Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is not centered. Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is centered.
Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is not centered. Screenshot of a slide in a Google Slides presentation. The slide contains a single image that is centered.

Conclusion

In this tutorial I showed you how to center images in a Google Slides presentation using Google Apps Script. Hope you found this tutorial helpful. Thanks for reading!


Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!