wsj1995 commited on
Commit
bfdbb2a
·
verified ·
1 Parent(s): bcf1951

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -40
app.py CHANGED
@@ -11,13 +11,12 @@ from oss2.credentials import EnvironmentVariableCredentialsProvider,StaticCreden
11
 
12
  # 下载网络图片
13
  def download_image(url):
14
- # 通过 URL 下载图片
15
- response = requests.get(url)
16
- if response.status_code == 200:
17
  return Image.open(BytesIO(response.content))
18
- else:
19
- raise Exception(f"Failed to download image, status code: {response.status_code}")
20
-
21
 
22
  def remove_metadata(image, output_path=None):
23
  # 获取图片的格式
@@ -47,48 +46,21 @@ def upload_to_oss(file_path, object_name, bucket_name, access_key_id, access_key
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):
 
11
 
12
  # 下载网络图片
13
  def download_image(url):
14
+ try:
15
+ response = requests.get(url, timeout=10) # 添加超时处理,避免长时间等待
16
+ response.raise_for_status() # 如果状态码不是200,抛出HTTPError
17
  return Image.open(BytesIO(response.content))
18
+ except requests.exceptions.RequestException as e:
19
+ raise RuntimeError(f"图片下载失败: {e}")
 
20
 
21
  def remove_metadata(image, output_path=None):
22
  # 获取图片的格式
 
46
  return None
47
 
48
  def compress_image(image, output_path, quality=85):
 
 
 
 
 
 
 
 
49
  img_format = image.format.lower()
50
 
51
+ if img_format in ['jpeg', 'webp']:
52
+ image.save(output_path, img_format.upper(), quality=quality)
 
 
 
 
53
  elif img_format == 'png':
54
+ image.save(output_path, 'PNG', optimize=True)
 
 
 
55
  elif img_format == 'gif':
 
56
  image = image.convert("P", palette=Image.ADAPTIVE)
57
  image.save(output_path, optimize=True)
 
 
58
  elif img_format == 'tiff':
59
+ image.save(output_path, 'TIFF', compression='tiff_deflate')
 
 
 
 
 
 
 
 
60
  else:
61
+ raise Exception(f"不支持的图片格式: {img_format}")
62
+
63
+ return output_path
 
64
 
65
  # 删除本地文件
66
  def delete_local_file(file_path):