0

I have a PowerPoint that contains an image with a transparent background. I want to use the python-pptx package to extract it, but it doesn't include the transparent background.

Basically I have an image variable which is a python-pptx PICTURE object.

        file = "test.png"
        image_bytes = image.image.blob
        image_stream = BytesIO(image_bytes)
        im = Image.open(image_stream)
        im.save(file, format="PNG")

But instead of an image with a transparent background I get a black background. Any help?

vdvaxel
  • 667
  • 1
  • 14
  • 41
  • What happens when you right-click the picture in PowerPoint and choose Save As Picture? Does the image still have a black background? Also, how can you tell the picture has a black background? What software are you using for that test? – scanny Aug 28 '19 at 16:41
  • Also see this link; https://forums.adobe.com/thread/2467831. In any case, I don't believe this has to do with `python-pptx` per se, rather it is an artifact of that particular PNG image. – scanny Aug 28 '19 at 16:45
  • Hello @scanny, many thanks for your response! I've indeed tried to manually Save As Picture and when I put it on my desktop it clearly has a transparent background (because I can see my wallpaper through it). However, when I extract it automatically using `python-pptx`, it shows a black background on my desktop for the same image. – vdvaxel Aug 29 '19 at 08:11
  • Hmm, just looking at your code, there is an unnecessary step. You can save the image-bytes directly to a file. Try `with open("test.png", "wb") as f: f.write(image.image_blob)` (instead of all five lines you show above). – scanny Aug 29 '19 at 17:45
  • Thanks @scanny! Unfortunately it doesn't solve the issue with the black background, though. – vdvaxel Aug 30 '19 at 09:02
  • Well, the process above doesn't transform the image in any way, it just dumps the image bytes as stored. I expect the problem is upstream. Maybe you can post-process the dump with ImageMaverick or something like that. – scanny Aug 30 '19 at 18:21

0 Answers0