save code
This commit is contained in:
+40
-2
@@ -10,6 +10,7 @@ from PIL import Image
|
||||
from datetime import datetime
|
||||
from volcenginesdkarkruntime import Ark
|
||||
import shutil
|
||||
import oss2
|
||||
|
||||
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
STATIC_FOLDER = os.path.join(APP_ROOT, 'static')
|
||||
@@ -115,7 +116,7 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, is_generated):
|
||||
return None, "get image type error"
|
||||
|
||||
if not is_generated:
|
||||
if cloth_len == '大腿' or cloth_len == '膝盖' or '小腿':
|
||||
if cloth_len == '大腿' or cloth_len == '膝盖' or cloth_len == '小腿':
|
||||
takeoff_file_name = takeoff_cloth_first(human_name)
|
||||
if takeoff_file_name == None:
|
||||
return None, f"takeoff_cloth_first error {human_name}"
|
||||
@@ -252,6 +253,38 @@ def image_to_base64(file_path, mime_type=None):
|
||||
# 组合成Data URI格式
|
||||
return f"data:{mime_type};base64,{encoded_string}"
|
||||
|
||||
def upload_to_oss(image_path, object_name=None):
|
||||
# 配置信息(替换为你的实际信息)
|
||||
access_key_id = 'LTAI5tB9t2RH6f1drSLvVLLZ'
|
||||
access_key_secret = '91uzPI1RAFHN7n3Y6TJDGFP8w0dG1R'
|
||||
endpoint = 'oss-cn-beijing.aliyuncs.com' # 替换为你的Endpoint
|
||||
bucket_name = 'llyz'
|
||||
|
||||
# 创建Bucket实例
|
||||
auth = oss2.Auth(access_key_id, access_key_secret)
|
||||
bucket = oss2.Bucket(auth, endpoint, bucket_name)
|
||||
|
||||
# 如果没有指定OSS文件名,则使用本地文件名
|
||||
if object_name is None:
|
||||
object_name = image_path.split('/')[-1] # 取本地文件名
|
||||
|
||||
try:
|
||||
# 上传文件
|
||||
bucket.put_object_from_file(object_name, image_path)
|
||||
|
||||
# 获取文件URL(有效期默认10年)
|
||||
url = bucket.sign_url('GET', object_name, 3600 * 24 * 365 * 10)
|
||||
|
||||
# 或者使用公共读Bucket的URL(如果Bucket是公共读权限)
|
||||
# url = f"https://{bucket_name}.{endpoint}/{object_name}"
|
||||
|
||||
print(f"文件上传成功,URL: {url}")
|
||||
return url
|
||||
except Exception as e:
|
||||
print(f"上传失败: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
def process_change_cloth(human_filename, cloth_filename, output_format, img_url, is_generated):
|
||||
w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}')
|
||||
|
||||
@@ -265,6 +298,11 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url,
|
||||
jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/imgs/{jpg_name}'
|
||||
image.save(jpg_path_name, quality=95)
|
||||
|
||||
|
||||
upload_to_oss(jpg_path_name, jpg_name) # 第二个参数可选,指定OSS上的路径
|
||||
https_url = f'https://llyz.oss-cn-beijing.aliyuncs.com/{jpg_name}'
|
||||
print(f"生成的HTTPS URL: {https_url}")
|
||||
|
||||
if 'base64' in output_format:
|
||||
return jsonify({
|
||||
"ret":0,
|
||||
@@ -275,7 +313,7 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url,
|
||||
return jsonify({
|
||||
"ret":0,
|
||||
"msg":"success",
|
||||
"url":f"http://112.126.94.241:18888/static/imgs/{jpg_name}"
|
||||
"url":https_url
|
||||
})
|
||||
|
||||
@app.route('/change_cloth_base64', methods=['POST'])
|
||||
|
||||
Reference in New Issue
Block a user