Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -46,6 +46,50 @@ def upload_to_oss(file_path, object_name, bucket_name, access_key_id, access_key
|
|
46 |
print(f"OSS上传失败: {e}")
|
47 |
return None
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# 删除本地文件
|
50 |
def delete_local_file(file_path):
|
51 |
if os.path.exists(file_path):
|
@@ -69,6 +113,8 @@ def run(params):
|
|
69 |
callback_header_key = params['callback_header_key']
|
70 |
callback_header_secret = params['callback_header_secret']
|
71 |
model_type = params['model_type']
|
|
|
|
|
72 |
# url,version_id,access_key_id,access_key_secret,securityToken,endpoint,bucket_name,upload_filename,callback_url,callback_header_key,callback_header_secret
|
73 |
# 网络图片 URL 和本地保存路径
|
74 |
tmp_file_name = uuid.uuid4()
|
@@ -78,8 +124,11 @@ def run(params):
|
|
78 |
downloaded_image = download_image(url)
|
79 |
|
80 |
if downloaded_image:
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
83 |
if access_key_id and endpoint and bucket_name:
|
84 |
print('上传OSS')
|
85 |
# 步骤 3:上传到阿里云 OSS
|
|
|
46 |
print(f"OSS上传失败: {e}")
|
47 |
return None
|
48 |
|
49 |
+
def compress_image(image, output_path, quality=85):
|
50 |
+
"""
|
51 |
+
根据图片格式自动选择压缩方法并压缩图片,保留元数据。
|
52 |
+
|
53 |
+
:param image: PIL Image 对象
|
54 |
+
:param output_path: 输出压缩后的图片文件路径
|
55 |
+
:param quality: 对于有损压缩的图片格式(JPEG, WebP)设置压缩质量,范围 1-100
|
56 |
+
"""
|
57 |
+
# 获取图片的实际格式
|
58 |
+
img_format = image.format.lower()
|
59 |
+
|
60 |
+
|
61 |
+
if img_format == 'jpeg':
|
62 |
+
# 压缩 JPEG,同时保留 EXIF 数据(如果有)
|
63 |
+
image.save(output_path, "JPEG", quality=quality)
|
64 |
+
print(f"Compressed JPEG with quality={quality}, EXIF data preserved.")
|
65 |
+
|
66 |
+
elif img_format == 'png':
|
67 |
+
# 压缩 PNG,optimize=True 启用无损压缩
|
68 |
+
image.save(output_path, "PNG", optimize=True)
|
69 |
+
print("Compressed PNG with lossless compression")
|
70 |
+
|
71 |
+
elif img_format == 'gif':
|
72 |
+
# 压缩 GIF,使用调色板模式减少颜色
|
73 |
+
image = image.convert("P", palette=Image.ADAPTIVE)
|
74 |
+
image.save(output_path, optimize=True)
|
75 |
+
print("Compressed GIF with optimized color palette")
|
76 |
+
|
77 |
+
elif img_format == 'tiff':
|
78 |
+
# 压缩 TIFF,使用 tiff_deflate 进行无损压缩
|
79 |
+
image.save(output_path, "TIFF", compression="tiff_deflate")
|
80 |
+
print("Compressed TIFF using deflate compression")
|
81 |
+
|
82 |
+
elif img_format == 'webp':
|
83 |
+
# WebP 有损压缩
|
84 |
+
image.save(output_path, "WEBP", quality=quality)
|
85 |
+
print(f"Compressed WebP with quality={quality}")
|
86 |
+
|
87 |
+
else:
|
88 |
+
raise Exception(f"Unsupported image format: {img_format}")
|
89 |
+
return
|
90 |
+
|
91 |
+
print(f"Image saved to {output_path}")
|
92 |
+
|
93 |
# 删除本地文件
|
94 |
def delete_local_file(file_path):
|
95 |
if os.path.exists(file_path):
|
|
|
113 |
callback_header_key = params['callback_header_key']
|
114 |
callback_header_secret = params['callback_header_secret']
|
115 |
model_type = params['model_type']
|
116 |
+
compress = params.get('compress',false)
|
117 |
+
quality = params.get('quality',85)
|
118 |
# url,version_id,access_key_id,access_key_secret,securityToken,endpoint,bucket_name,upload_filename,callback_url,callback_header_key,callback_header_secret
|
119 |
# 网络图片 URL 和本地保存路径
|
120 |
tmp_file_name = uuid.uuid4()
|
|
|
124 |
downloaded_image = download_image(url)
|
125 |
|
126 |
if downloaded_image:
|
127 |
+
if compress:
|
128 |
+
output_image_path = compress_image(downloaded_image,result_image)
|
129 |
+
else:
|
130 |
+
output_image_path = remove_metadata(downloaded_image,result_image)
|
131 |
+
print("元数据移除完成")
|
132 |
if access_key_id and endpoint and bucket_name:
|
133 |
print('上传OSS')
|
134 |
# 步骤 3:上传到阿里云 OSS
|