'Import images, such as a logo or person image
filename     a CSV list of records and filenames
tablename    the name of the table
idfield      the field used to identify the record
imagefield   the field to where the image will be added
Example:
bi.import_image ("org_logos.csv", "org_organisation", "name", "logo")
and the file org_logos.csv may look as follows
id                            file
Sahana Software Foundation    sahanalogo.jpg
American Red Cross            icrc.gif'

def import_image(self, filename, tablename, idfield, imagefield):

    try:
        openFile = open(filename, 'r')
    except IOError:
        return ('Unable    to    open    file    %s' % filename)
    (prefix, name) = tablename.split('_', 1)
    reader = self.csv.DictReader(openFile)
    db = current.db
    s3db = current.s3db
    audit = current.audit
    table = s3db[tablename]
    idfield = table[idfield]
    base_query = (table.deleted != True)
    get_config = s3db.get_config
    onvalidation = (get_config(tablename, 'update_onvalidation') or get_config(tablename, 'onvalidation'))
    onaccept = (get_config(tablename, 'update_onaccept') or get_config(tablename, 'onaccept'))
    update_realm = get_config(tablename, 'update_realm')
    if update_realm:
        set_realm_entity = current.auth.set_realm_entity
    update_super = s3db.update_super
    for row in reader:
        if (row != None):
            image = row['file']
            try:
                path = os.path.split(filename)[0]
                imagepath = os.path.join(path, image)
                openFile = open(imagepath, 'rb')
            except IOError:
                current.log.error(('Unable    to    open    image    file    %s' % image))
                continue
            image_source = StringIO(openFile.read())
            query = (base_query & (idfield == row['id']))
            record = db(query).select(limitby=(0, 1)).first()
            try:
                record_id = record.id
            except:
                current.log.error(('Unable    to    get    record    %s    of    the    resource    %s    to    attach    the    image    file    to' % (row['id'], tablename)))
                continue
            form = SQLFORM(table, record, fields=['id', imagefield])
            form_vars = Storage()
            form_vars._formname = ('%s/%s' % (tablename, record_id))
            form_vars.id = record_id
            source = Storage()
            source.filename = imagepath
            source.file = image_source
            form_vars[imagefield] = source
            if form.accepts(form_vars, onvalidation=onvalidation):
                audit('update', prefix, name, form=form, record=record_id, representation='csv')
                update_super(table, form_vars)
                if update_realm:
                    set_realm_entity(table, form_vars, force_update=True)
                callback(onaccept, form, tablename=tablename)
            else:
                for (key, error) in form.errors.items():
                    current.log.error(('error    importing    logo    %s:    %s    %s' % (image, key, error)))
