[497bce]: / mongo_db.py

Download this file

29 lines (22 with data), 645 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import io
import pymongo as pymongo
from PIL import Image
myclient = pymongo.MongoClient(
"mongo_uri")
# database creation
db = myclient["Endoscopic_Guidance"]
def convert_image_to_byte_array(file):
im = Image.open(file)
image_bytes = io.BytesIO()
im.save(image_bytes, format='JPEG')
image = {
'data': image_bytes.getvalue()
}
return image
def upload_image_data_mongodb(file):
images = db.images
# mongodb server
imgByteArr = convert_image_to_byte_array(file=file)
# database creation
image_id = images.insert_one(imgByteArr).inserted_id
print(image_id, 'Successfully inserted')