<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def export_img(visio_filename, image_filename, pagenum=None, pagename=None):
 """ Exports images from visio file """

    # visio requires absolute path
    image_pathname = os.path.abspath(image_filename)

    if not os.path.isdir(os.path.dirname(image_pathname)):
        msg = 'Could not write image file: %s' % image_filename
        raise IOError(msg)

    with VisioFile.Open(visio_filename) as visio:
        pages = filter_pages(visio.pages, pagenum, pagename)
        try:
            if len(pages) == 1:
                pages[0].Export(image_pathname)
            else:
                digits = int(log(len(pages), 10)) + 1
                basename, ext = os.path.splitext(image_pathname)
                filename_format = "%s%%0%dd%s" % (basename, digits, ext)

                for i, page in enumerate(pages):
                    filename = filename_format % (i + 1)
                    page.Export(filename)
        except Exception:
            raise IOError('Could not write image: %s' % image_pathname)