From 9ba12d06da32dcc26ba8dc7cc73ca187c3cd97c9 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Tue, 22 Aug 2023 10:33:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20global=20img=20=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ecm/service.py | 3 +++ apps/ecm/tasks.py | 10 +++++++++- apps/utils/img.py | 26 ++++++++++++++++---------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/apps/ecm/service.py b/apps/ecm/service.py index 1635c8ff..15164a58 100644 --- a/apps/ecm/service.py +++ b/apps/ecm/service.py @@ -27,6 +27,7 @@ from apps.utils.tools import timestamp_to_time from apps.third.tapis import xxapis import logging from datetime import datetime +from apps.utils.img import compress_image myLogger = logging.getLogger('log') requests.packages.urllib3.disable_warnings() @@ -58,6 +59,8 @@ def save_dahua_pic(pic_url: str, save_path: str = '/media/'): os.makedirs(full_path) with open(full_path + file_name, 'wb') as f: f.write(res.content) + full_path_file = full_path + file_name + compress_image(full_path_file) return path + file_name diff --git a/apps/ecm/tasks.py b/apps/ecm/tasks.py index 20b4ec8c..b2ee03a8 100644 --- a/apps/ecm/tasks.py +++ b/apps/ecm/tasks.py @@ -24,6 +24,7 @@ from datetime import timedelta from apps.ai.main import algo_dict from datetime import datetime import uuid +from apps.utils.img import compress_image @shared_task(base=CustomTask) def store_img(code: str, duration: int): @@ -192,4 +193,11 @@ def monitor_and_analyse(vchannel_code: str, algo_codes: list): # 释放资源 cap.release() - cv2.destroyAllWindows() \ No newline at end of file + cv2.destroyAllWindows() + +@shared_task(base=CustomTask) +def compressed_all_ecm_image(): + events = Event.objects.exclude(global_img=None) + for event in events: + if event.global_img: + compress_image(settings.BASE_DIR + event.global_img) \ No newline at end of file diff --git a/apps/utils/img.py b/apps/utils/img.py index 4e104f28..416e22f6 100644 --- a/apps/utils/img.py +++ b/apps/utils/img.py @@ -1,7 +1,7 @@ import os from PIL import Image -def compress_image(infile, outfile='', kb=80, step=10, quality=80): +def compress_image(infile, outfile='', kb=80, quality=80): """不改变图片尺寸压缩到指定大小 :param infile: 压缩源文件 :param outfile: 压缩文件保存地址 @@ -14,12 +14,18 @@ def compress_image(infile, outfile='', kb=80, step=10, quality=80): if o_size <= kb: return infile if outfile == '': - outfile = infile - while o_size > kb: - im = Image.open(infile) - im.save(outfile, quality=quality) - if quality - step < 0: - break - quality -= step - o_size = os.path.getsize(outfile)/1024 - return outfile, o_size \ No newline at end of file + path, end = infile.split('.') + outfile = path + '_compressed.' + end + im = Image.open(infile) + im.save(outfile, quality=quality) + while os.path.getsize(outfile) / 1024 > kb: + imx = Image.open(outfile) + # Resize the image using the same aspect ratio to reduce the file size + width, height = imx.size + new_width = int(width * 0.9) # You can adjust the scaling factor + new_height = int(height * 0.9) + imx = imx.resize((new_width, new_height), Image.ANTIALIAS) + imx.save(outfile, quality=quality) + # quality -= step + + return outfile, os.path.getsize(outfile) / 1024 \ No newline at end of file