预览图片等比缩小,jpg质量减小

This commit is contained in:
zqc
2026-03-10 10:40:22 +08:00
parent d87e960e8b
commit 03caa122ec

View File

@@ -81,7 +81,16 @@ class BaseFrameProcessorWorker(threading.Thread):
def _encode_image_to_base64(self, img) -> str:
"""图像编码为 Base64"""
ok, buf = cv2.imencode(".jpg", img)
# 检查并调整图片尺寸
h, w = img.shape[:2]
if h > 720:
new_h = 720
new_w = int(w * (720 / h))
img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
# 设置JPEG质量参数
params = [int(cv2.IMWRITE_JPEG_QUALITY), 80] # 质量0.8
ok, buf = cv2.imencode(".jpg", img, params)
if not ok:
raise RuntimeError("Failed to encode image to JPEG")
return base64.b64encode(buf.tobytes()).decode("ascii")