save code

This commit is contained in:
colomi
2025-07-11 22:57:10 +08:00
parent 4ca55230e8
commit d4a5739887
2 changed files with 236 additions and 3 deletions
+9 -3
View File
@@ -73,7 +73,7 @@ def GetPicDesc(img_url):
return result return result
# ComfyUI 的 input 目录路径(根据实际修改) # ComfyUI 的 input 目录路径(根据实际修改)
COMFYUI_INPUT_DIR = "/path/to/ComfyUI/input" COMFYUI_INPUT_DIR = "/home/szlc/code/ComfyUI/input"
os.makedirs(COMFYUI_INPUT_DIR, exist_ok=True) os.makedirs(COMFYUI_INPUT_DIR, exist_ok=True)
def get_file_extension(url_or_filename): def get_file_extension(url_or_filename):
@@ -148,8 +148,11 @@ def change(human_name, cloth_name, c_width, c_height):
print("History:", history) print("History:", history)
outputs = history[prompt_id]["outputs"] outputs = history[prompt_id]["outputs"]
for out in outputs: for out in outputs:
if out_img_name in out: if 'images' in outputs[out]:
return out for out_img_name in outputs[out]['images']:
if 'filename' in out_img_name:
return out_img_name['filename']
return None
@app.route('/change_cloth', methods=['POST']) @app.route('/change_cloth', methods=['POST'])
def change_cloth(): def change_cloth():
@@ -279,6 +282,9 @@ def change_cloth_base64():
w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}') w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}')
out_put_name = change(human_filename, cloth_filename, w, h) out_put_name = change(human_filename, cloth_filename, w, h)
if out_put_name == None:
return jsonify({"ret":-1, 'msg': 'Failed to change cloth'}), 500
image = Image.open(f'/home/szlc/code/ComfyUI/output/{out_put_name}') image = Image.open(f'/home/szlc/code/ComfyUI/output/{out_put_name}')
jpg_name = out_put_name.replace(".png", ".jpg") jpg_name = out_put_name.replace(".png", ".jpg")
jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/{jpg_name}' jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/{jpg_name}'
+227
View File
@@ -0,0 +1,227 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>服装更换工具</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
h1 {
text-align: center;
color: #333;
}
.image-upload-container {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.image-upload-box {
width: 48%;
border: 2px dashed #ccc;
padding: 15px;
text-align: center;
border-radius: 5px;
}
.image-upload-box h3 {
margin-top: 0;
}
.image-preview {
max-width: 100%;
max-height: 300px;
margin-top: 10px;
display: none;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
display: block;
margin: 20px auto;
}
button:hover {
background-color: #45a049;
}
#result {
margin-top: 20px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 4px;
display: none;
}
#loading {
text-align: center;
display: none;
margin: 20px 0;
}
.spinner {
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 2s linear infinite;
margin: 0 auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>服装更换工具</h1>
<div class="image-upload-container">
<div class="image-upload-box">
<h3>人体图片</h3>
<input type="file" id="humanInput" accept="image/*">
<img id="humanPreview" class="image-preview" alt="人体图片预览">
</div>
<div class="image-upload-box">
<h3>衣服图片</h3>
<input type="file" id="clothInput" accept="image/*">
<img id="clothPreview" class="image-preview" alt="衣服图片预览">
</div>
</div>
<button id="submitBtn">提交处理</button>
<div id="loading">
<div class="spinner"></div>
<p>正在处理中,请稍候...</p>
</div>
<div id="result"></div>
<script>
// 图片预览功能
document.getElementById('humanInput').addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
const img = document.getElementById('humanPreview');
img.src = event.target.result;
img.style.display = 'block';
};
reader.readAsDataURL(file);
}
});
document.getElementById('clothInput').addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
const img = document.getElementById('clothPreview');
img.src = event.target.result;
img.style.display = 'block';
};
reader.readAsDataURL(file);
}
});
// 提交处理
document.getElementById('submitBtn').addEventListener('click', function() {
const humanFile = document.getElementById('humanInput').files[0];
const clothFile = document.getElementById('clothInput').files[0];
if (!humanFile || !clothFile) {
alert('请同时选择人体图片和衣服图片!');
return;
}
// 显示加载动画
document.getElementById('loading').style.display = 'block';
document.getElementById('result').style.display = 'none';
// 读取图片并转换为Base64(包含完整前缀)
Promise.all([
readFileAsDataURL(humanFile),
readFileAsDataURL(clothFile)
]).then(([humanDataURL, clothDataURL]) => {
// 准备请求数据
const data = {
human_img: humanDataURL, // 包含完整前缀的Base64
cloth_img: clothDataURL, // 包含完整前缀的Base64
output_format: "url"
};
console.log("准备发送的数据:", data); // 调试用
// 调用API
fetch('http://112.126.94.241:18018/change_cloth_base64', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP错误! 状态: ${response.status}`);
}
return response.json();
})
.then(data => {
// 隐藏加载动画
document.getElementById('loading').style.display = 'none';
// 显示结果
const resultDiv = document.getElementById('result');
if (data.url) {
resultDiv.innerHTML = `
<h3>处理结果</h3>
<p>处理成功!点击查看结果:</p>
<a href="${data.url}" target="_blank">${data.url}</a>
<p><img src="${data.url}" style="max-width: 100%; margin-top: 10px;"></p>
`;
} else {
resultDiv.innerHTML = `
<h3>处理结果</h3>
<p class="error">处理完成,但未返回预期的URL</p>
<p>返回数据:</p>
<pre>${JSON.stringify(data, null, 2)}</pre>
`;
}
resultDiv.style.display = 'block';
})
.catch(error => {
console.error('Error:', error);
document.getElementById('loading').style.display = 'none';
document.getElementById('result').innerHTML = `
<h3 class="error">错误</h3>
<p>处理过程中出现错误:${error.message}</p>
<p>请检查控制台获取更多信息</p>
`;
document.getElementById('result').style.display = 'block';
});
});
});
// 辅助函数:读取文件为DataURL(包含完整前缀的Base64
function readFileAsDataURL(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
reader.readAsDataURL(file);
});
}
</script>
</body>
</html>