135 lines
5.1 KiB
Python
135 lines
5.1 KiB
Python
import os
|
|
|
|
from lib.all import *
|
|
from PIL import Image
|
|
import oss2
|
|
|
|
service_name = 'vas_deal'
|
|
log_dir = '/app/log'
|
|
logger = Logger(service_name, log_dir=log_dir)
|
|
|
|
access_key_id = "LTAI5tAdu5LRvZwm4LJa21Fo"
|
|
access_key_secret = "WGvSQsDralTfFAAxhEqLBOgbXqflHo"
|
|
endpoint_internal = "https://oss-cn-hangzhou-internal.aliyuncs.com"
|
|
bucket_name = "wishpal-ironfan-media"
|
|
|
|
HorizontalView = 1 # 横图
|
|
VerticalView = 2 # 竖图
|
|
|
|
Resize720P = 1280
|
|
Resize1080P = 1920
|
|
Resize1440P = 2560
|
|
|
|
|
|
def calc_new_file_size(img_size: tuple, resize_p: int = Resize720P):
|
|
w = int(img_size[0])
|
|
h = int(img_size[1])
|
|
|
|
view_type = HorizontalView if w > h else VerticalView
|
|
|
|
if view_type == HorizontalView:
|
|
reduce_rate = safe_div(resize_p, w)
|
|
new_w = int(float(w) * reduce_rate)
|
|
new_h = int(float(h) * reduce_rate)
|
|
return new_w, new_h
|
|
else:
|
|
reduce_rate = safe_div(resize_p, h)
|
|
new_w = int(float(w) * reduce_rate)
|
|
new_h = int(float(h) * reduce_rate)
|
|
return new_w, new_h
|
|
|
|
|
|
def compress_and_save_image(img_path: str, dst_path: str, resize_p: int):
|
|
try:
|
|
img = Image.open(img_path)
|
|
resize_img = img.resize((calc_new_file_size(img.size, resize_p)))
|
|
resize_img.save(dst_path)
|
|
return "success"
|
|
except Exception as e:
|
|
return str(e)
|
|
|
|
|
|
class S:
|
|
def __init__(self):
|
|
self.bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint_internal, bucket_name)
|
|
self.col_image = MongoDB(
|
|
host="mongodb://root:Wishpal2024@dds-bp1628b447c242e41.mongodb.rds.aliyuncs.com:3717,dds-bp1628b447c242e42.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-74245203",
|
|
port=3717,
|
|
db="media",
|
|
collection="image"
|
|
)
|
|
print(self.col_image)
|
|
|
|
def __del__(self):
|
|
self.col_image.close()
|
|
|
|
def save_img_from_oss(self, oss_src_id: str, local_path: str):
|
|
return self.bucket.get_object_to_file(oss_src_id, local_path)
|
|
|
|
def upload_img_to_oss(self, local_path: str, oss_src_id: str):
|
|
return self.bucket.put_object_from_file(oss_src_id, local_path)
|
|
|
|
def proc(self):
|
|
images = [
|
|
{
|
|
"src_id": "imgprod/23/f8/9b54-b9f0-4add-9024-7eb06b0ba0bf"
|
|
}
|
|
]
|
|
for image in images:
|
|
src_id = safe_get_str(image, "src_id")
|
|
src_id_python_type = src_id.replace("/", "_")
|
|
|
|
# 当前目录
|
|
cur_dir = os.getcwd() + "/"
|
|
|
|
# 先下载到本地,源文件
|
|
oss_src_download_path = cur_dir + src_id_python_type + ".jpeg"
|
|
try:
|
|
self.save_img_from_oss(src_id, oss_src_download_path)
|
|
src_size = os.path.getsize(oss_src_download_path)
|
|
except Exception as e:
|
|
logger.Error("save_img_from_oss fail, err: {}, src: {}".format(str(e), src_id))
|
|
continue
|
|
|
|
# 源文件转720P
|
|
oss_720_src_id = src_id.replace("imgprod", "imgprod720")
|
|
local_720p_path = cur_dir + src_id_python_type.replace("imgprod", "imgprod720") + ".jpeg"
|
|
ret720 = compress_and_save_image(oss_src_download_path, local_720p_path, Resize720P)
|
|
if ret720 != "success":
|
|
logger.Error("compress_and_save_image 720 fail, err: {}, src: {}, local: {}".format(ret720, oss_src_download_path, local_720p_path))
|
|
continue
|
|
try:
|
|
ret = self.upload_img_to_oss(local_720p_path, oss_720_src_id)
|
|
if ret.status != 200:
|
|
logger.Error("upload_img_to_oss 720 fail, err: {}, src: {}, local: {}".format(ret720, oss_src_download_path, local_720p_path))
|
|
continue
|
|
# 更新mongo
|
|
|
|
except Exception as e:
|
|
logger.Error("upload_img_to_oss 720 fail, err: {}, src: {}, local: {}".format(str(e), oss_src_download_path, local_720p_path))
|
|
continue
|
|
|
|
# 源文件转1080P
|
|
oss_1080_src_id = src_id.replace("imgprod", "imgprod1080")
|
|
local_1080p_path = cur_dir + src_id_python_type.replace("imgprod", "imgprod1080") + ".jpeg"
|
|
ret1080 = compress_and_save_image(oss_src_download_path, local_1080p_path, Resize1080P)
|
|
if ret1080 != "success":
|
|
logger.Error("compress_and_save_image 1080 fail, err: {}, src: {}, local: {}".format(ret1080, oss_src_download_path, local_1080p_path))
|
|
continue
|
|
ret = self.upload_img_to_oss(local_1080p_path, oss_1080_src_id)
|
|
print(ret.status)
|
|
|
|
# 源文件转1440P
|
|
oss_1440_src_id = src_id.replace("imgprod", "imgprod1440")
|
|
local_1440p_path = cur_dir + src_id_python_type.replace("imgprod", "imgprod1440") + ".jpeg"
|
|
ret1440 = compress_and_save_image(oss_src_download_path, local_1440p_path, Resize1440P)
|
|
if ret1440 != "success":
|
|
logger.Error("compress_and_save_image 1440 fail, err: {}, src: {}, local: {}".format(ret1440, oss_src_download_path, local_1440p_path))
|
|
continue
|
|
ret = self.upload_img_to_oss(local_1440p_path, oss_1440_src_id)
|
|
print(ret.status)
|
|
|
|
|
|
s = S()
|
|
s.proc()
|