Skip to content

Instantly share code, notes, and snippets.

@Apoorvgarg-creator
Created November 7, 2021 11:18
Show Gist options
  • Save Apoorvgarg-creator/6fe996dbbab9df964802dd7f5d7d2d49 to your computer and use it in GitHub Desktop.
Save Apoorvgarg-creator/6fe996dbbab9df964802dd7f5d7d2d49 to your computer and use it in GitHub Desktop.
This script uploads all the Images to the cloud server "Cloudinary", and add the cloudinary link to the MongoDB server with other properties for fast extraction
import os
import requests
import base64
import json
url_upload = "https://blitz-db-service.herokuapp.com/upload"
url_add_products = "https://blitz-db-service.herokuapp.com/product"
dataset_path = "/Users/apoorvgarg/Downloads/Myntra-HackerRamp"
dataset_images = os.path.join(dataset_path,"/images")
for folder_name in os.listdir(dataset_images):
if folder_name == ".DS_Store":
continue
f_n = folder_name.split("_")
color = f_n[0]
type_ = f_n[1]
subtype_ = f_n[2]
for file_name in os.listdir(os.path.join(dataset_images,folder_name)):
if file_name == ".DS_Store":
continue
img_path = os.path.join(dataset_images,folder_name,file_name)
with open(img_path,"rb") as img_f:
b64_String = base64.b64encode(img_f.read())
b64_= "data:image/png;base64," + str(b64_String.decode('utf-8'))
body_upload = { "image" : b64_ }
r_upload = requests.post(url=url_upload,json=body_upload)
url_cloudinary = r_upload.content.decode('utf-8')
body_add_products = {
"name" : file_name,
"image_url" : url_cloudinary,
"type": type_,
"sub_type": subtype_,
"color": color,
"is_tagged": True
}
r_add_products = requests.post(url_add_products,json = body_add_products)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment