Last modified: Aug 23, 2026
Python PPTX Add Picture Guide
Adding images to PowerPoint slides is a common task. The add_picture method in python-pptx makes this simple. It lets you place images anywhere on a slide. You can also control the size and scaling. This guide shows you how to use it effectively.
This method is part of the python-pptx library. It works with both .pptx files and in-memory slide objects. You can add pictures from local files or from streams. The method returns a Picture object. This object gives you full control over the image's properties.
Basic Syntax of add_picture
The core function is straightforward. You call it on a slide object. You must provide the image file path. You can also specify left and top positions. These are in English Metric Units (EMU). The default position is the top-left corner of the slide.
from pptx import Presentation
from pptx.util import Inches
# Create a presentation
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[6]) # Blank layout
# Add a picture at position (1 inch, 1 inch)
pic = slide.shapes.add_picture('image.jpg', Inches(1), Inches(1))
The example above adds an image at a specific location. The Inches() helper converts inches to EMU. You can also use Pt() for points or Cm() for centimeters. The returned pic object can be modified later.
Controlling Picture Size
You can set the width and height of the image. The method accepts these as optional parameters. If you provide only width, the height scales proportionally. If you provide only height, the width scales proportionally. Providing both will stretch the image to fit.
# Add picture with specific width (height auto-scales)
pic = slide.shapes.add_picture('image.jpg', Inches(1), Inches(1), width=Inches(4))
# Add picture with specific height (width auto-scales)
pic = slide.shapes.add_picture('image.jpg', Inches(1), Inches(1), height=Inches(3))
# Add picture with fixed width and height (stretched)
pic = slide.shapes.add_picture('image.jpg', Inches(1), Inches(1), width=Inches(5), height=Inches(2))
Use the proportional scaling to avoid distortion. For example, if you want a 4-inch wide image, just set the width. The library calculates the correct height. This preserves the aspect ratio. It's the best way to keep images looking natural.
Positioning Images Precisely
The left and top parameters are mandatory. They define the top-left corner of the image. You can calculate positions dynamically. This helps place images relative to other elements. For instance, you can center an image on a slide.
from pptx.util import Inches, Emu
# Get slide dimensions
slide_width = prs.slide_width
slide_height = prs.slide_height
# Image size (assume 2x2 inches)
img_width = Inches(2)
img_height = Inches(2)
# Calculate center position
left = Emu(int((slide_width - img_width) / 2))
top = Emu(int((slide_height - img_height) / 2))
# Add centered picture
pic = slide.shapes.add_picture('image.jpg', left, top, width=img_width, height=img_height)
This code centers the image horizontally and vertically. It uses Emu for precise arithmetic. The slide dimensions are in EMU. This approach works for any slide size. It's a common pattern for creating polished presentations.
Adding Pictures from a Stream
Sometimes you have image data in memory. You might download an image from the web. Or you might generate it with PIL. The add_picture method accepts a file-like object. This is useful for dynamic content.
import requests
from io import BytesIO
from pptx import Presentation
# Download an image
response = requests.get('https://example.com/image.png')
image_stream = BytesIO(response.content)
# Create presentation and slide
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[6])
# Add picture from stream
pic = slide.shapes.add_picture(image_stream, Inches(1), Inches(1), width=Inches(3))
The stream must support seek() and read() methods. A BytesIO object works perfectly. This method is efficient. It avoids saving temporary files to disk. It's great for automation scripts that fetch images on the fly.
Working with Picture Object Properties
After adding a picture, you can modify it. The returned Picture object has useful properties. You can change its position or size later. You can also apply cropping. This makes the method very flexible.
# Add a picture
pic = slide.shapes.add_picture('image.jpg', Inches(1), Inches(1), width=Inches(4))
# Change position later
pic.left = Inches(2)
pic.top = Inches(2)
# Change size later
pic.width = Inches(6)
pic.height = Inches(3)
# Crop from left (10% of original width)
pic.crop_left = 0.1
print(f"Picture added at left={pic.left}, top={pic.top}")
print(f"Size: {pic.width} x {pic.height}")
Picture added at left=1828800, top=1828800
Size: 5486400 x 2743200
The output shows EMU values. The crop property accepts floats from 0.0 to 1.0. This represents the fraction to crop from that side. You can crop from all four sides. This is useful for trimming images without editing the source file.
Handling Common Errors
You might encounter errors when using add_picture. The most common is a FileNotFoundError. This happens when the image path is wrong. Always check the file path before running the code. Use absolute paths or ensure the working directory is correct.
try:
pic = slide.shapes.add_picture('missing.jpg', Inches(1), Inches(1))
except FileNotFoundError as e:
print(f"Error: {e}")
Another issue is an UnsupportedImageError. This occurs when the file is not a valid image. python-pptx supports PNG, JPEG, GIF, and BMP. If you get this error, check the file format. Convert the image to a supported format first.
Best Practices for Image Handling
Always validate your images before adding them. Check the file size and dimensions. Large images can slow down your presentation. Consider resizing images before adding them. This improves performance and reduces file size.
For a consistent look, define a standard image size. Use it across all slides. This makes your presentation look professional. You can create helper functions to wrap add_picture calls. This reduces code duplication and makes maintenance easier.
If you are working with templates, this method works seamlessly. You can add pictures to any slide in a template. This is useful for creating branded reports. For more advanced template usage, check our guide on Python PPTX: Use Template for Easy Slides. It explains how to use placeholders effectively.
Example: Creating a Photo Grid
Let's build a small photo grid. This demonstrates multiple pictures on one slide. We'll use a loop to place images in a 2x2 grid. This is a common layout for comparison slides.
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[6])
images = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg']
positions = [
(Inches(0.5), Inches(0.5)),
(Inches(4.5), Inches(0.5)),
(Inches(0.5), Inches(4.5)),
(Inches(4.5), Inches(4.5)),
]
for img, (left, top) in zip(images, positions):
slide.shapes.add_picture(img, left, top, width=Inches(3.5))
prs.save('photo_grid.pptx')
print("Photo grid created successfully!")
Photo grid created successfully!
This code places four images in a grid. Each image is 3.5 inches wide. The positions are hardcoded. You can easily modify this to create different layouts. The grid pattern is a great way to present multiple visuals.
Conclusion
The add_picture method is essential for any python-pptx user. It provides full control over image placement and size. You can work with files or streams. You can also modify pictures after adding them. This makes it a versatile tool for presentation automation.
Remember to handle errors gracefully. Always check your image paths. Use proportional scaling to avoid distortion. With these techniques, you can create visually appealing slides. Start adding images to your presentations today. Your audience will appreciate the enhanced content.