How to add images to plone 5

I am trying to import images into my plone 5 instance and here is my code:

if obj_format=="image/jpeg" or obj_format=="image/gif" or obj_format=="image/png":
    filename = d["File"]
    filepath = json_file_path
    container=portal
    destination = traverse_dir(absolute_url, container)

    #upload file
    file_to_upload = open(os.path.join(filepath, filename), mode='rb')

    blob = NamedBlobImage(
    data=file_to_upload,
    contentType=obj_format,
    filename=filename)

    api.content.create(
    type='Image',
    container=destination,
    image=blob,
    title=filename,
    safe_id=False)

    file_to_upload.close()
    transaction.commit()

However the images are not showing and I get this error: Image cannot be displayed.
Am I doing the import correctly?

Instead of passing the image parameter to api.content.create method, we assing the value to the image field afterwards:

obj = api.content.create(...)
obj.image = NamedBlobImage(filedata, filename=filename)
1 Like