cszhzleo commited on
Commit
1c873bf
1 Parent(s): aab208b

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +30 -0
utils.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ from io import BytesIO
3
+ from PIL import Image
4
+
5
+
6
+ def convert_to_base64(image_file_path):
7
+ """
8
+ Convert PIL images to Base64 encoded strings
9
+
10
+ :param pil_image: PIL image
11
+ :return: Re-sized Base64 string
12
+ """
13
+
14
+ pil_image = Image.open(image_file_path)
15
+
16
+ buffered = BytesIO()
17
+ pil_image.save(buffered, format="png") # You can change the format if needed
18
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
19
+ return img_str
20
+
21
+
22
+ def convert_to_html(img_base64):
23
+ """
24
+ Disply base64 encoded string as image
25
+
26
+ :param img_base64: Base64 string
27
+ """
28
+ # Create an HTML img tag with the base64 string as the source
29
+ image_html = f'<img src="data:image/jpeg;base64,{img_base64}" style="max-width: 100%;"/>'
30
+ return image_html