feat(notification): 新增图片压缩处理
- 新增 ImageUtils.compress_image_if_needed 方法,用于压缩图片大小 - 在 MAA.py 和 notification.py 中集成图片压缩功能 - 添加对不同图片格式(JPEG、PNG)的压缩支持 - 优化图片路径处理,确保压缩后图片正确发送 - 更新 requirements.txt,添加 pillow 依赖
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import os
|
||||
from PIL import Image
|
||||
|
||||
class ImageUtils:
|
||||
@staticmethod
|
||||
@@ -19,3 +21,51 @@ class ImageUtils:
|
||||
"""从base64字符串计算md5"""
|
||||
image_data = base64.b64decode(base64_content)
|
||||
return hashlib.md5(image_data).hexdigest()
|
||||
|
||||
@staticmethod
|
||||
def compress_image_if_needed(image_path, max_size_mb=2):
|
||||
"""
|
||||
如果图片大于max_size_mb(默认2MB),则压缩到max_size_mb以内,
|
||||
返回压缩后文件路径(压缩文件与原图同目录,命名为xxx_compressed.xxx),
|
||||
如果不超过则返回原文件路径
|
||||
"""
|
||||
|
||||
if hasattr(Image, "Resampling"): # Pillow 9.1.0及以后
|
||||
RESAMPLE = Image.Resampling.LANCZOS
|
||||
else: # Pillow 老版本
|
||||
RESAMPLE = Image.ANTIALIAS
|
||||
|
||||
max_size = max_size_mb * 1024 * 1024
|
||||
file_size = os.path.getsize(image_path)
|
||||
if file_size <= max_size:
|
||||
return image_path # 小于2MB,直接返回原路径
|
||||
|
||||
# 只支持JPEG、PNG压缩
|
||||
file_root, file_ext = os.path.splitext(image_path)
|
||||
compressed_path = f"{file_root}_compressed{file_ext}"
|
||||
|
||||
img = Image.open(image_path)
|
||||
quality = 90 if file_ext.lower() in ['.jpg', '.jpeg'] else None
|
||||
step = 5 # 每次降低5质量或5%尺寸
|
||||
|
||||
# 如果是JPG/JPEG
|
||||
if file_ext.lower() in ['.jpg', '.jpeg']:
|
||||
while True:
|
||||
img.save(compressed_path, quality=quality, optimize=True)
|
||||
if os.path.getsize(compressed_path) <= max_size or quality <= 10:
|
||||
break
|
||||
quality -= step
|
||||
# PNG 只能调整尺寸来压缩
|
||||
elif file_ext.lower() == '.png':
|
||||
width, height = img.size
|
||||
while True:
|
||||
img.save(compressed_path, optimize=True)
|
||||
if os.path.getsize(compressed_path) <= max_size or width <= 200 or height <= 200:
|
||||
break
|
||||
width = int(width * 0.95)
|
||||
height = int(height * 0.95)
|
||||
img = img.resize((width, height), RESAMPLE)
|
||||
else:
|
||||
raise ValueError("仅支持JPG/JPEG和PNG格式图片的压缩。")
|
||||
|
||||
return compressed_path
|
||||
|
||||
Reference in New Issue
Block a user