Upload Image in Erpnext

Im Trying to upload image captured through web camera or mobile camera to erpnext. the data is a base64 string which im passing to python api. where im decoding the base64 string data by using base64 python module.

image_data = “data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAElEQVR4nHS791eT6fb/7T/1fc6ZsVDTGxB6kybFAlZ67yggvYeEmgAhjd5BOvbeu446OjrqWBDI6/khDDNzzudkrb2ukMVKsu5X3vu9976ue5fuRCqtp==”

data = base64.b64decode(image_data)

but keeping getting an error : binascii.Error: Incorrect padding

Hey @Yashwanth, maybe you can use function provided by Frappe

from frappe.utils.file_manager import save_file
sf = save_file(fname, content, dt, dn)
# fname = filename
# content = your image in base64
# dt = the doctype
# dn = the docname

This will upload the image and create a File Database on Frappe.
Image uploaded on /frappe-bench/sites/your-sitename/public

1 Like

@antzforwork thank you and i have an other question for you, I want to render the same image in my doctype but in File doctype i find only the url so do i need to encode/decode the file and render it on UI or is there a function for that.

this worked but the Image is not reflecting in File Doctype but its available in database

You will have to upload image through UI as it sets URL for it. I also uploaded 3000 product images directly to the image directory via SSH. But couldn’t link it to Item. In the end had to upload via UI and write a script to match image name with item name. FYI image name was set as item name before uploading hence the script worked.