question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature: Picture.replace_image()

See original GitHub issue

As discussed on the mailing group (https://groups.google.com/forum/?hl=en#!topic/python-pptx/YJBzJznTTHw), there are some potential uses for replacing an image with a new one. [Think “refreshing” a PPT with updated images.]

Some working code (largely suggested by Steve) is as follows:

from pptx import Presentation
from pptx.util import Px
from PIL import Image

# inputs
PPT_TEMPLATE = 'test.pptx'
REPLACEMENT_IMG = 'pic.png'

# load preso and shape
presso = Presentation(PPT_TEMPLATE)
slide = presso.slides[0]
img = slide.shapes[0]

# get current image info
imgPic = img._pic
imgRID = imgPic.xpath('./p:blipFill/a:blip/@r:embed', namespaces=imgPic.nsmap)[0]
imgPart = slide.related_parts[imgRID]

# get info about replacement image
with open(REPLACEMENT_IMG, 'rb') as f:
    rImgBlob = f.read()
rImgWidth, rImgHeight = Image.open(REPLACEMENT_IMG).size
rImgWidth, rImgHeight = Px(rImgWidth), Px(rImgHeight) # change from Px

# replace
imgPart._blob = rImgBlob

# now alter the size and position to suit that of the replacement img
# rescale sizes so image isn't stretched
widthScale = float(rImgWidth) / img.width
heightScale = float(rImgHeight) / img.height
maxScale = max(widthScale, heightScale)
scaledImgWidth, scaledImgHeight = int(rImgWidth / maxScale), int(rImgHeight / maxScale)
# center the image if it's different size to the original
scaledImgLeft = int(img.left + (img.width - scaledImgWidth)/2)
scaledImgTop = int(img.top + (img.height - scaledImgHeight)/2)
# now update
img.left, img.top, img.width, img.height = scaledImgLeft, scaledImgTop, scaledImgWidth, scaledImgHeight

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:3
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
timinicommented, Sep 4, 2018

+1 would like this feature

3reactions
lokesh1729commented, Aug 7, 2020

to those who wants a solution, summing up from above comments

there are two solutions

Solution 1

new_shape = slide_obj.shapes.add_picture(
    img_location,
    Inches(shape_left),
    Inches(shape_top),
    Inches(shape_width),
    Inches(shape_height),
)
old_pic = shape_obj._element
new_pic = new_shape._element
old_pic.addnext(new_pic)
old_pic.getparent().remove(old_pic)

slide_obj -> is the slide where you want to add picture

img_location -> path to the image file

shape_obj -> existing picture shape object

shape_left, shape_top, shape_width, shape_height -> are self explanatory

Solution 2:

def _replace_picture(self, slide_obj, shape_obj, img_location):
    # noinspection PyProtectedMember
    with open(img_location, "rb") as file_obj:
        r_img_blob = file_obj.read()
    img_pic = shape_obj._pic
    img_rid = img_pic.xpath("./p:blipFill/a:blip/@r:embed")[0]
    img_part = slide_obj.part.related_parts[img_rid]
    img_part._blob = r_img_blob

in this method we’re replacing blob, there’ll be a problem with this method when you have a requirement to replicate current slide in a loop.

For example, let’s say you want to replicate a slide containing image for 5 times. Each time you want to put 5 different images namely a, b, c, d, e

So, what’ll happen with this method is you’ll see the last image i.e. ‘e’ in all 5 slides. why ? because we’re replacing blob so the latest blob will be put everywhere

Read more comments on GitHub >

github_iconTop Results From Across the Web

Replace or delete a picture in Microsoft Office
From the menu that appears, select where you want to get the replacement picture from. Navigate to the image you want, and then...
Read more >
Replace image - Pixelmator Pro User Guide
Use the Replace Image feature to quickly swap one image for another while preserving the layer styles, color adjustments, effects, and other nondestructive ......
Read more >
Change Picture from clipboard in Word and Office
'Change Picture' lets you smoothly replace an existing image with another while keeping much the same image settings. If the clipboard item is ......
Read more >
Replace Picture - UiPath Documentation Portal
List OCR/Image Activities. Container Usage ... Match Function. Protect Sheet ... Replace. Text to Left/Right. Add Data Column. Add Data Row.
Read more >
Find and replace image path - GIS Stack Exchange
path.exists() (and os.path.isfile()) to test that your pathNEW actually points to a file before setting the new path to pic.sourceImage.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found