优化图片的存储,及时删除处理过程中产生的图片

This commit is contained in:
2024-10-15 10:17:01 +08:00
parent 15ea3ff96f
commit 5af6256376
5 changed files with 141 additions and 94 deletions

View File

@@ -1,13 +1,10 @@
import logging
import math
import os
import cv2
import numpy
import requests
from tenacity import retry, stop_after_attempt, wait_random
from PIL import Image
from log import PROJECT_ROOT
from util import common_util
def capture(image, rectangle):
@@ -42,14 +39,14 @@ def split(img_path, ratio=1.414, overlap=0.05, x_compensation=3):
hw_ratio = height / width
wh_ratio = width / height
img_name, img_ext = parse_save_path(img_path)
img_name, img_ext = common_util.parse_save_path(img_path)
if hw_ratio > ratio: # 纵向过长
new_img_height = width * ratio
step = width * (ratio - overlap) # 偏移步长
for i in range(math.ceil(height / step)):
offset = round(step * i)
cropped_img = capture(image, [0, offset, width, offset + new_img_height])
split_path = get_save_path(f'{img_name}.split_{i}.{img_ext}')
split_path = common_util.get_processed_img_path(f'{img_name}.split_{i}.{img_ext}')
cv2.imwrite(split_path, cropped_img)
split_result.append({'img': split_path, 'x_offset': 0, 'y_offset': offset})
elif wh_ratio > ratio: # 横向过长
@@ -58,7 +55,7 @@ def split(img_path, ratio=1.414, overlap=0.05, x_compensation=3):
for i in range(math.ceil(width / step)):
offset = round(step * i)
cropped_img = capture(image, [offset, 0, offset + new_img_width, width])
split_path = get_save_path(f'{img_name}.split_{i}.{img_ext}')
split_path = common_util.get_processed_img_path(f'{img_name}.split_{i}.{img_ext}')
cv2.imwrite(split_path, cropped_img)
split_result.append({'img': split_path, 'x_offset': offset, 'y_offset': 0})
else:
@@ -111,8 +108,8 @@ def rotate(img_path, angle):
# 参数:原始图像 旋转参数 元素图像宽高
rotated = cv2.warpAffine(image, matrix, (new_width, new_height))
img_name, img_ext = parse_save_path(img_path)
rotated_path = get_save_path(f'{img_name}.rotate_{angle}.{img_ext}')
img_name, img_ext = common_util.parse_save_path(img_path)
rotated_path = common_util.get_processed_img_path(f'{img_name}.rotate_{angle}.{img_ext}')
cv2.imwrite(rotated_path, rotated)
return rotated_path
@@ -178,7 +175,7 @@ def expand_to_a4_size(img_path):
:return: 扩充后的图片NumPy数组和偏移量
"""
image = cv2.imread(img_path)
img_name, img_ext = parse_save_path(img_path)
img_name, img_ext = common_util.parse_save_path(img_path)
height, width = image.shape[:2]
x_offset, y_offset = 0, 0
hw_ratio = height / width
@@ -206,8 +203,9 @@ def expand_to_a4_size(img_path):
exp_img = numpy.zeros((y_offset, width, 3), dtype='uint8')
exp_img.fill(255)
image = numpy.vstack([exp_img, image, exp_img])
# todo:未拓展时不要生成新的图片
save_path = get_save_path(f'{img_name}.a4.{img_ext}')
else:
return img_path, 0, 0
save_path = common_util.get_processed_img_path(f'{img_name}.a4.{img_ext}')
cv2.imwrite(save_path, image)
return save_path, x_offset, y_offset
@@ -240,45 +238,16 @@ def combined(img1, img2):
return combined_img
def parse_img_url(url):
def is_photo(img_path):
"""
解析图片url
:param url: 图片url
:return: 图片名称和图片后缀
是否是拍照照片
:param img_path: 图片路径
:return: True:是照片False:可能不是照片,也可能在传输过程中相机等信息丢失了
"""
url = url.split('?')[0]
return os.path.basename(url)
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('保存图片失败!'))
def save_to_local(img_url):
"""
保存图片到本地
:param img_url: 图片url
:return: 本地保存地址
"""
response = requests.get(img_url)
response.raise_for_status() # 检查响应状态码是否正常
save_path = get_save_path(parse_img_url(img_url))
with open(save_path, 'wb') as file:
file.write(response.content)
return save_path
def get_img_path(img_full_name):
save_path = get_save_path(img_full_name)
if os.path.exists(save_path):
return save_path
return None
def get_save_path(img_full_name):
return os.path.join(PROJECT_ROOT, 'tmp_img', img_full_name)
def parse_save_path(img_path):
img_full_name = os.path.basename(img_path)
img_name, img_ext = img_full_name.rsplit('.', 1)
return img_name, img_ext
img = Image.open(img_path)
exif = img.getexif()
if exif:
# 271:相机制造商, 272:相机型号
if any(tag in exif for tag in (271, 272)):
return True
return False