aifeifei798 commited on
Commit
d241648
1 Parent(s): fbec79f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -1,4 +1,6 @@
 
1
  import base64
 
2
  import os
3
  from mistralai import Mistral
4
  import gradio as gr
@@ -84,12 +86,19 @@ def infer(prompt, quality_select, styles_Radio, FooocusExpansion_select, seed=42
84
  def encode_image(image_path):
85
  """Encode the image to base64."""
86
  try:
87
- with open(image_path, "rb") as image_file:
88
- return base64.b64encode(image_file.read()).decode("utf-8")
 
 
 
 
 
 
 
89
  except FileNotFoundError:
90
  print(f"Error: The file {image_path} was not found.")
91
  return None
92
- except Exception as e: # Added general exception handling
93
  print(f"Error: {e}")
94
  return None
95
 
 
1
+ from PIL import Image
2
  import base64
3
+ from io import BytesIO
4
  import os
5
  from mistralai import Mistral
6
  import gradio as gr
 
86
  def encode_image(image_path):
87
  """Encode the image to base64."""
88
  try:
89
+ # 打开图片文件
90
+ image = Image.open(image_path).convert("RGB")
91
+
92
+ # 将图片转换为字节流
93
+ buffered = BytesIO()
94
+ image.save(buffered, format="JPEG")
95
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
96
+
97
+ return img_str
98
  except FileNotFoundError:
99
  print(f"Error: The file {image_path} was not found.")
100
  return None
101
+ except Exception as e: # 添加通用异常处理
102
  print(f"Error: {e}")
103
  return None
104