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