walidadebayo commited on
Commit
7c3493b
·
1 Parent(s): 311bfbf

Normalize float types to uint8 in image_to_base64 function

Browse files
Files changed (1) hide show
  1. app.py +5 -0
app.py CHANGED
@@ -70,6 +70,11 @@ def base64_to_image(base64_str):
70
  return img
71
 
72
  def image_to_base64(image):
 
 
 
 
 
73
  # Convert to BGR if it's RGB
74
  if len(image.shape) > 2 and image.shape[2] == 3:
75
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
 
70
  return img
71
 
72
  def image_to_base64(image):
73
+ # Convert float types to uint8
74
+ if image.dtype == np.float64 or image.dtype == np.float32:
75
+ # Normalize to 0-255 range and convert to uint8
76
+ image = np.clip(image * 255 if image.max() <= 1.0 else image, 0, 255).astype(np.uint8)
77
+
78
  # Convert to BGR if it's RGB
79
  if len(image.shape) > 2 and image.shape[2] == 3:
80
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)