Failed to Extract WMF Images from Shapes in the PowerPoint Presentation in Python

PFA - pdf i used to extract images, screenshots of the error.

The code i used is the one mentioned in the documentation.

It is not converting imagetype : x-wmf to jpeg and is failing to save it.
PDF:
pptexamples.zip (1.0 MB)

@Naveenj,
Thank you for contacting support.

Please try using the following code example:

import aspose.pydrawing as draw
import aspose.slides as slides

# Please note that the get_image_format and get_file_extension must be consistent by keys.

def get_image_format(image_type):
    return {
        "jpeg": draw.imaging.ImageFormat.jpeg,
        "emf": draw.imaging.ImageFormat.emf,
        "bmp": draw.imaging.ImageFormat.bmp,
        "png": draw.imaging.ImageFormat.png,
        "wmf": draw.imaging.ImageFormat.wmf,
        "x-wmf": draw.imaging.ImageFormat.wmf,
        "gif": draw.imaging.ImageFormat.gif,
    }.get(image_type, draw.imaging.ImageFormat.jpeg)


def get_file_extension(image_type):
    return {
        "jpeg": "jpg",
        "emf": "emf",
        "bmp": "bmp",
        "png": "png",
        "wmf": "wmf",
        "x-wmf": "wmf",
        "gif": "gif",
    }.get(image_type, "jpg")


file_name = "BackImage_Slide_{0}{1}.{2}"

with slides.Presentation("pptexamples.ppt") as presentation:
    for slide in presentation.slides:
        back_image = None
        is_layout = False

        if slide.background.fill_format.fill_type == slides.FillType.PICTURE:
            back_image = slide.background.fill_format.picture_fill_format.picture.image
        elif slide.layout_slide.background.fill_format.fill_type == slides.FillType.PICTURE:
            back_image = slide.layout_slide.background.fill_format.picture_fill_format.picture.image
            is_layout = True

        if back_image is not None:
            image_type = back_image.content_type.split("/")[1]
            image_format = get_image_format(image_type)
            file_extension = get_file_extension(image_type)

            back_image.system_image.save(
                file_name.format("LayoutSlide_" if is_layout else "", slide.slide_number, file_extension),
                image_format)

        for shape_index, shape in enumerate(slide.shapes):
            shape_image = None

            if type(shape) is slides.AutoShape and shape.fill_format.fill_type == slides.FillType.PICTURE:
                shape_image = shape.fill_format.picture_fill_format.picture.image
            elif type(shape) is slides.PictureFrame:
                shape_image = shape.picture_format.picture.image

            if shape_image is not None:
                image_type = shape_image.content_type.split("/")[1]
                image_format = get_image_format(image_type)
                file_extension = get_file_extension(image_type)

                shape_image.system_image.save(
                    file_name.format("shape_" + str(shape_index) + "_", slide.slide_number, file_extension),
                    image_format)

You can also extract all images from presentation resources like this:

import aspose.pydrawing as draw
import aspose.slides as slides

# Please note that the get_image_format and get_file_extension must be consistent by keys.

def get_image_format(image_type):
    return {
        "jpeg": draw.imaging.ImageFormat.jpeg,
        "emf": draw.imaging.ImageFormat.emf,
        "bmp": draw.imaging.ImageFormat.bmp,
        "png": draw.imaging.ImageFormat.png,
        "wmf": draw.imaging.ImageFormat.wmf,
        "x-wmf": draw.imaging.ImageFormat.wmf,
        "gif": draw.imaging.ImageFormat.gif,
    }.get(image_type, draw.imaging.ImageFormat.jpeg)


def get_file_extension(image_type):
    return {
        "jpeg": "jpg",
        "emf": "emf",
        "bmp": "bmp",
        "png": "png",
        "wmf": "wmf",
        "x-wmf": "wmf",
        "gif": "gif",
    }.get(image_type, "jpg")


file_name = "image_{0}.{1}"

with slides.Presentation("pptexamples.ppt") as presentation:
    for image_index, image in enumerate(presentation.images):
        image_type = image.content_type.split("/")[1]
        image_format = get_image_format(image_type)
        file_extension = get_file_extension(image_type)

        image.system_image.save(
            file_name.format(image_index, file_extension),
            image_format)

It is still giving an error:
image_code3.png (93.5 KB)

@Naveenj,
Please indicate the OS version on which the error occurred.

os version.png (35.9 KB)

@Naveenj,
Thank you for the additional information. I’ve reproduced the error you are encountering.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESPYNET-133

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@Naveenj,
There is no implementation of encoders for WMF/EMF images in .NET, so you get the error on Linux. The PPImage class contains the binary_data property, and you can save raw data of images. Please consider a solution like this:

import logging
import aspose.slides as slides

def get_file_extension(image_type):
    return {
        "jpeg": "jpg",
        "emf": "emf",
        "bmp": "bmp",
        "png": "png",
        "wmf": "wmf",
        "x-wmf": "wmf",
        "gif": "gif",
    }.get(image_type, "")


presentation_path = "pptexamples.ppt"
file_name_template = "image_{0}.{1}"

with slides.Presentation(presentation_path) as presentation:
    for image_index, image in enumerate(presentation.images):
        image_type = image.content_type.split("/")[1]
        file_extension = get_file_extension(image_type)

        if not file_extension:
            message = "Failed to extract an '{0}' image.".format(image_type)
            logging.exception(message)
        else:
            file_name = file_name_template.format(image_index, file_extension)
            with open(file_name, 'wb') as image_file:
                image_file.write(image.binary_data)