ysharma HF staff commited on
Commit
ff6ac62
·
1 Parent(s): bf83dea
Files changed (1) hide show
  1. gradio_seg2image.py +12 -6
gradio_seg2image.py CHANGED
@@ -9,13 +9,18 @@ from PIL import Image
9
  import numpy as np
10
  import base64
11
 
12
- def encode(input_image):
13
  print(f"type of input_image ^^ - {type(input_image)}")
14
- # Convert NumPy array to bytes
15
- img_bytes = np.ndarray.tobytes(input_image)
16
 
17
- # Encode the bytes using Base64
18
- encoded_string = base64.b64encode(img_bytes).decode('utf-8')
 
 
 
 
 
19
 
20
  # Print and return the encoded string
21
  #print(encoded_string)
@@ -23,8 +28,9 @@ def encode(input_image):
23
 
24
  def create_imgcomp(input_image, filename):
25
  encoded_string = encode(input_image)
26
- htmltag = '<img src= "data:image/jpeg;base64,' + encoded_string + '" alt="Original Image"/></div> <img src= "https://ysharma-ControlNetSegmentation.hf.space/file=' + filename + '" alt="Control Net Image"/>'
27
  #https://ysharma-controlnetsegmentation.hf.space/file=/tmp/tmpqcz9yeta.png
 
28
  print(f"htmltag is ^^ - {htmltag}")
29
  desc = """
30
  <!DOCTYPE html>
 
9
  import numpy as np
10
  import base64
11
 
12
+ def encode(img_array):
13
  print(f"type of input_image ^^ - {type(input_image)}")
14
+ # Convert NumPy array to image
15
+ img = Image.fromarray(img_array)
16
 
17
+ # Save image to file
18
+ img_path = "temp_image.jpeg"
19
+ img.save(img_path)
20
+
21
+ # Encode image file using Base64
22
+ with open(img_path, "rb") as image_file:
23
+ encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
24
 
25
  # Print and return the encoded string
26
  #print(encoded_string)
 
28
 
29
  def create_imgcomp(input_image, filename):
30
  encoded_string = encode(input_image)
31
+ htmltag = '<img src= "data:image/jpeg;base64,' + encoded_string + '" alt="Original Image"/></div> <img src= "https://ysharma-controlnet-image-comparison.hf.space/file=' + filename + '" alt="Control Net Image"/>'
32
  #https://ysharma-controlnetsegmentation.hf.space/file=/tmp/tmpqcz9yeta.png
33
+ #https://ysharma-controlnet-image-comparison.hf.space/file=/tmp/tmpg4qx22xy.png
34
  print(f"htmltag is ^^ - {htmltag}")
35
  desc = """
36
  <!DOCTYPE html>