def _add_image(image, caption, context, width=None, height=None):
    # Load the image to find its size
    encoding = stdout.encoding if stdout.encoding is not None else "utf-8"
    try:
        image_file = open(image.encode(encoding), 'rb')
        image_object = Image.open(image_file)
    except (UnicodeEncodeError, IOError, OverflowError), e:
        printwarn('unable to insert the image "%s": %s' % (image, e))
        return
    size = image_object.size

    # Convert pixels to inches
    if width:
        try:
            width = int(width.replace('px', ''))
        except ValueError:
            raise NotImplementedError, 'only pixel units supported'
        if height:
            try:
                height = int(height)
            except ValueError:
                raise NotImplementedError, 'only pixel units supported'
        else:
            height = int(width / (float(size[0]) / float(size[1])))
        size = (width, height)
    elif height:
        try:
            height = int(height.replace('px', ''))
        except ValueError:
            raise NotImplementedError, 'only pixel units supported'
        width = int(height * (float(size[0]) / float(size[1])))
        size = (width, height)
    size = ("%sin" % (float(size[0]) / DPI), "%sin" % (float(size[1]) / DPI))

    # Add the image
    local_uri = context["doc"].add_file(image)

    # Frame style for the caption frame
    caption_style = _get_caption_style(context).get_style_name()
    # Frame style for the image frame
    image_style = _get_image_style(context).get_style_name()

    # In text application, image must be inserted in a paragraph
    if context["top"].get_tag() == "office:text":
        container = odf_create_paragraph()
        context["top"].append(container)
    else:
        container = context["top"]

    if caption:
        paragraph = odf_create_paragraph()
        image_frame = odf_create_image_frame(local_uri, size=size,
                style=image_style)
        paragraph.append(image_frame)
        paragraph.append(caption)
        # A new frame, we fix only the width
        text_frame = odf_create_text_frame(paragraph, size=(size[0], None),
                style=caption_style)
        container.append(text_frame)
    else:
        image_frame = odf_create_image_frame(local_uri, size=size,
                style=image_style)
        container.append(image_frame)