From dc20904f19a2032ce8b979a20402d1c2a76434dc Mon Sep 17 00:00:00 2001 From: colomi <1421901449@qq.com> Date: Sat, 12 Jul 2025 09:56:34 +0800 Subject: [PATCH] save code --- http_app.py | 175 +++++++++++++++++++++++++++------------------------- 1 file changed, 90 insertions(+), 85 deletions(-) diff --git a/http_app.py b/http_app.py index 9d2666a..5e25621 100644 --- a/http_app.py +++ b/http_app.py @@ -72,42 +72,6 @@ def GetPicDesc(img_url): return result -# ComfyUI 的 input 目录路径(根据实际修改) -COMFYUI_INPUT_DIR = "/home/szlc/code/ComfyUI/input" -os.makedirs(COMFYUI_INPUT_DIR, exist_ok=True) - -def get_file_extension(url_or_filename): - """从 URL 或文件名中提取扩展名(如 .jpg、.png)""" - # 处理 URL 情况(如 https://example.com/image.jpg?width=200) - if url_or_filename.startswith(('http://', 'https://')): - parsed = urlparse(url_or_filename) - path = parsed.path - else: - path = url_or_filename - - # 提取扩展名(转换为小写,去掉问号后的参数) - ext = os.path.splitext(path)[1].lower().split('?')[0] - return ext if ext else '.png' # 默认 PNG(如果无法提取) - -def save_image_from_url(image_url): - """从 URL 下载图片并保留原始格式""" - try: - response = requests.get(image_url, stream=True) - response.raise_for_status() - - # 获取原始图片格式 - ext = get_file_extension(image_url) - filename = f"{uuid.uuid4()}{ext}" - save_path = os.path.join(COMFYUI_INPUT_DIR, filename) - - with open(save_path, "wb") as f: - for chunk in response.iter_content(1024): - f.write(chunk) - - return filename - except Exception as e: - print(f"Error saving image from URL: {e}") - return None def change(human_name, cloth_name, c_width, c_height): with open('/home/szlc/code/ComfyUI/change_cloth/change_new.json', 'r', encoding='utf-8') as file: @@ -154,32 +118,6 @@ def change(human_name, cloth_name, c_width, c_height): return out_img_name['filename'] return None -@app.route('/change_cloth', methods=['POST']) -def change_cloth(): - """从 URL 下载图片""" - data = request.json - human_url = data.get("human_url") - - if not human_url: - return jsonify({"error": "Missing 'human_url' parameter"}), 400 - - human_filename = save_image_from_url(human_url) - if not human_filename: - return jsonify({"error": "Failed to download or save human image"}), 500 - - cloth_url = data.get("cloth_url") - - if not cloth_url: - return jsonify({"error": "Missing 'cloth_url' parameter"}), 400 - - cloth_filename = save_image_from_url(cloth_url) - if not cloth_filename: - return jsonify({"error": "Failed to download or save image"}), 500 - - - - return jsonify({"filename": cloth_filename}) - def save_base64_image(base64_str, prefix): """ 将base64字符串保存为图片文件 @@ -254,6 +192,31 @@ def image_to_base64(file_path, mime_type=None): # 组合成Data URI格式 return f"data:{mime_type};base64,{encoded_string}" +def process_change_cloth(human_filename, cloth_filename, output_format): + w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}') + + 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}') + jpg_name = out_put_name.replace(".png", ".jpg") + jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/{jpg_name}' + image.save(jpg_path_name, quality=95) + + if 'base64' in output_format: + return jsonify({ + "ret":0, + "msg":"success", + "data":image_to_base64(jpg_path_name) + }) + else: + return jsonify({ + "ret":0, + "msg":"success", + "url":f"http://112.126.94.241:18018/static/{jpg_name}" + }) + @app.route('/change_cloth_base64', methods=['POST']) def change_cloth_base64(): # 获取参数 @@ -279,32 +242,74 @@ def change_cloth_base64(): if not cloth_filename: return jsonify({"ret":-1, 'msg': 'Failed to save cloth image'}), 500 - w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}') - - 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}') - jpg_name = out_put_name.replace(".png", ".jpg") - jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/{jpg_name}' - image.save(jpg_path_name, quality=95) - - if 'base64' in output_format: - return jsonify({ - "ret":0, - "msg":"success", - "data":image_to_base64(jpg_path_name) - }) - else: - return jsonify({ - "ret":0, - "msg":"success", - "url":f"http://112.126.94.241:18018/static/{jpg_name}" - }) + return process_change_cloth(human_filename, cloth_filename, output_format) except Exception as e: return jsonify({"ret":-1, 'error': str(e)}), 500 + +def get_file_extension(url_or_filename): + """从 URL 或文件名中提取扩展名(如 .jpg、.png)""" + # 处理 URL 情况(如 https://example.com/image.jpg?width=200) + if url_or_filename.startswith(('http://', 'https://')): + parsed = urlparse(url_or_filename) + path = parsed.path + else: + path = url_or_filename + + # 提取扩展名(转换为小写,去掉问号后的参数) + ext = os.path.splitext(path)[1].lower().split('?')[0] + return ext if ext else '.png' # 默认 PNG(如果无法提取) + +def save_image_from_url(image_url): + """从 URL 下载图片并保留原始格式""" + try: + response = requests.get(image_url, stream=True) + response.raise_for_status() + + # 获取原始图片格式 + ext = get_file_extension(image_url) + filename = f"{uuid.uuid4()}{ext}" + filepath = os.path.join('/home/szlc/code/ComfyUI/change_cloth/static', filename) + + with open(filepath, "wb") as f: + for chunk in response.iter_content(1024): + f.write(chunk) + + input_filepath = os.path.join('/home/szlc/code/ComfyUI/input', filename) + with open(input_filepath, "wb") as f: + for chunk in response.iter_content(1024): + f.write(chunk) + + return filename + except Exception as e: + print(f"Error saving image from URL: {e}") + return None + +@app.route('/change_cloth', methods=['POST']) +def change_cloth(): + """从 URL 下载图片""" + data = request.json + human_url = data.get("human_url") + + if not human_url: + return jsonify({"error": "Missing 'human_url' parameter"}), 400 + + human_filename = save_image_from_url(human_url) + if not human_filename: + return jsonify({"error": "Failed to download or save human image"}), 500 + + cloth_url = data.get("cloth_url") + + if not cloth_url: + return jsonify({"error": "Missing 'cloth_url' parameter"}), 400 + + cloth_filename = save_image_from_url(cloth_url) + if not cloth_filename: + return jsonify({"error": "Failed to download or save image"}), 500 + + output_format = data.get('output_format') + + return process_change_cloth(human_filename, cloth_filename, output_format) if __name__ == '__main__': app.run(host="0.0.0.0", port=8018, debug=True) \ No newline at end of file