import base64import jsonimport uuidimport osfrom os import listdirfile_path="C:/Users/***/Documents/Projects/Falsk_API_Img2Base64/image/"output_path="base64_txt_folder/"Unique_ID = uuid.uuid1()def create_image_file(images): input_path=os.path.join(file_path, images) with open(input_path, "rb") as image2string: converted_string = base64.b64encode(image2string.read()).decode('ascii') payload = json.dumps({"imagedata": converted_string}) file = open(output_path+'image_'+str(Unique_ID)+'.txt', 'w') file.write(payload) file.close() for images in os.listdir(file_path): create_image_file(images)
I am trying to encode a set of images (100 images in folder) to base64 format and save it in text format individually in the output folder. This code is taking only one image for converting it to base64 and exiting from the loop. It is not taking the next image for conversion. The requirement is, if I run this code, it has to take one image at a time and convert it into base64 format and save that string in JSON format in a text file, and then it has to take 2nd image to conversion and save it in text format and so on till the last image in the folder.