Use free API endpoint for background removal tool (no authentication required)

- Switch to remove_background_from_file_free endpoint
- Remove auth headers and credentials from API call
This commit is contained in:
jingrow 2025-12-20 21:45:24 +08:00
parent 6292b8b510
commit 8a4431075a
2 changed files with 28 additions and 45 deletions

View File

@ -560,25 +560,13 @@ const handleRemoveBackground = async () => {
resultImage.value = ''
try {
const reader = new FileReader()
const base64Promise = new Promise<string>((resolve, reject) => {
reader.onload = (e) => {
const result = e.target?.result as string
resolve(result)
}
reader.onerror = reject
})
reader.readAsDataURL(uploadedImage.value)
const base64Data = await base64Promise
const formData = new FormData()
formData.append('file', uploadedImage.value)
const response = await axios.post(
'/jingrow.tools.remove_background.remove_background.remove_background_from_file',
'/jingrow.tools.remove_background.remove_background.remove_background_from_file_free',
formData,
{
image_data: base64Data
},
{
headers: get_session_api_headers(),
withCredentials: true,
timeout: 180000
}
)

View File

@ -1,9 +1,8 @@
# Copyright (c) 2025, JINGROW and contributors
# For license information, please see license.txt
import base64
import requests
from typing import Dict, Any, Union
from typing import Dict, Any
import logging
import io
@ -137,13 +136,13 @@ def remove_background_from_file_free(file) -> Dict[str, Any]:
@jingrow.whitelist()
def remove_background_from_file(image_data: Union[str, list]) -> Dict[str, Any]:
def remove_background_from_file(file) -> Dict[str, Any]:
"""
图片去背景工具
调用 Jingrow Cloud API 实现图片背景移除
Args:
image_data: Base64编码的图片数据data:image/开头
file: 上传的文件对象multipart/form-data
Returns:
dict: 处理结果
@ -158,41 +157,37 @@ def remove_background_from_file(image_data: Union[str, list]) -> Dict[str, Any]:
"error": "API密钥未设置请在设置中配置 Jingrow Cloud Api Key 和 Jingrow Cloud Api Secret"
}
# 处理单个数据
if isinstance(image_data, list):
image_data = image_data[0] if image_data else None
# 读取文件内容
file.file.seek(0)
file_content = file.file.read()
if not image_data:
if not file_content or len(file_content) == 0:
return {
"success": False,
"error": "未提供图片数据"
"error": "文件内容为空"
}
# 提取base64部分
if ',' in image_data:
header, base64_content = image_data.split(',', 1)
if 'png' in header:
ext = 'png'
content_type = 'image/png'
elif 'jpeg' in header or 'jpg' in header:
ext = 'jpg'
content_type = 'image/jpeg'
elif 'webp' in header:
ext = 'webp'
content_type = 'image/webp'
else:
ext = 'png'
content_type = 'image/png'
# 获取文件扩展名和内容类型
filename = getattr(file, 'filename', None) or 'image.png'
ext = filename.split('.')[-1].lower() if '.' in filename else 'png'
if ext == 'jpg':
ext = 'jpg'
content_type = 'image/jpeg'
elif ext == 'jpeg':
ext = 'jpg'
content_type = 'image/jpeg'
elif ext == 'png':
ext = 'png'
content_type = 'image/png'
elif ext == 'webp':
ext = 'webp'
content_type = 'image/webp'
else:
base64_content = image_data
ext = 'png'
content_type = 'image/png'
# 解码base64数据
image_bytes = base64.b64decode(base64_content)
# 处理EXIF orientation
image = Image.open(io.BytesIO(image_bytes))
image = Image.open(io.BytesIO(file_content))
image = ImageOps.exif_transpose(image)
output = io.BytesIO()
save_format = image.format or content_type.split('/')[-1].upper()