JuanjoSG5
commited on
Commit
·
a941c51
1
Parent(s):
2ccc1a1
feat: added the change format function, and a visulize image function
Browse files- mcp_server.py +7 -2
- src/utils/change_format.py +32 -0
- src/utils/visualize_image.py +18 -0
mcp_server.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
from mcp.server.fastmcp import FastMCP
|
2 |
-
from src.
|
|
|
|
|
|
|
3 |
mcp = FastMCP("Youtube Service")
|
4 |
|
5 |
@mcp.tool()
|
@@ -15,7 +18,9 @@ def say_hello(name: str) -> str:
|
|
15 |
"""
|
16 |
return f"Hello, {name}!"
|
17 |
|
18 |
-
mcp.add_tool(
|
|
|
|
|
19 |
|
20 |
if __name__ == "__main__":
|
21 |
mcp.run()
|
|
|
1 |
from mcp.server.fastmcp import FastMCP
|
2 |
+
from src.utils.change_format import change_format
|
3 |
+
from src.utils.image_helpers import remove_background_from_url
|
4 |
+
from src.utils.visualize_image import visualize_base64_image
|
5 |
+
|
6 |
mcp = FastMCP("Youtube Service")
|
7 |
|
8 |
@mcp.tool()
|
|
|
18 |
"""
|
19 |
return f"Hello, {name}!"
|
20 |
|
21 |
+
mcp.add_tool(remove_background_from_url)
|
22 |
+
mcp.add_tool(change_format)
|
23 |
+
mcp.add_tool(visualize_base64_image)
|
24 |
|
25 |
if __name__ == "__main__":
|
26 |
mcp.run()
|
src/utils/change_format.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from io import BytesIO
|
3 |
+
import requests
|
4 |
+
import base64
|
5 |
+
|
6 |
+
def change_format(image_url: str, target_format: str) -> str:
|
7 |
+
"""
|
8 |
+
Change the format of an image from a URL to the specified target format.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
image_url: The URL of the input image.
|
12 |
+
target_format: The desired output format (e.g., 'JPEG', 'PNG').
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
The image converted to the target format as a base64-encoded string.
|
16 |
+
"""
|
17 |
+
|
18 |
+
response = requests.get(image_url, timeout=30)
|
19 |
+
response.raise_for_status()
|
20 |
+
|
21 |
+
# Open the image from bytes
|
22 |
+
img = Image.open(BytesIO(response.content))
|
23 |
+
|
24 |
+
# Convert the image to the target format
|
25 |
+
output = BytesIO()
|
26 |
+
img.save(output, format=target_format)
|
27 |
+
output.seek(0)
|
28 |
+
|
29 |
+
# Convert to base64 string for JSON serialization
|
30 |
+
encoded_image = base64.b64encode(output.getvalue()).decode('utf-8')
|
31 |
+
|
32 |
+
return encoded_image # Return base64 encoded string that can be serialized to JSON
|
src/utils/visualize_image.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
|
5 |
+
def visualize_base64_image(base64_string:str):
|
6 |
+
"""
|
7 |
+
Visualize a base64-encoded image string.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
base64_string: The base64-encoded image string.
|
11 |
+
"""
|
12 |
+
# Decode the base64 string back to binary
|
13 |
+
image_data = base64.b64decode(base64_string)
|
14 |
+
|
15 |
+
# Create an image from the binary data
|
16 |
+
img = Image.open(BytesIO(image_data))
|
17 |
+
|
18 |
+
img.show()
|