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 = '' resultImage.value = ''
try { try {
const reader = new FileReader() const formData = new FormData()
const base64Promise = new Promise<string>((resolve, reject) => { formData.append('file', uploadedImage.value)
reader.onload = (e) => {
const result = e.target?.result as string
resolve(result)
}
reader.onerror = reject
})
reader.readAsDataURL(uploadedImage.value)
const base64Data = await base64Promise
const response = await axios.post( 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 timeout: 180000
} }
) )

View File

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