This commit is contained in:
lwl0608 2024-02-06 01:57:17 +08:00
parent 4693bb6e87
commit bdc7b66c6b
1 changed files with 39 additions and 16 deletions

View File

@ -10,15 +10,15 @@ 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"
endpoint_internal = "https://oss-cn-hangzhou.aliyuncs.com"
bucket_name = "wishpal-ironfan-media"
HorizontalView = 1 # 横图
VerticalView = 2 # 竖图
Resize720P = 1280
Resize1080P = 1920
Resize1440P = 2560
Resize720P = 720
Resize1080P = 1080
Resize1440P = 1440
def calc_new_file_size(img_size: tuple, resize_p: int = Resize720P):
@ -27,13 +27,22 @@ def calc_new_file_size(img_size: tuple, resize_p: int = Resize720P):
view_type = HorizontalView if w > h else VerticalView
if resize_p == Resize720P:
resize_format = 1280
elif resize_p == Resize1080P:
resize_format = 1920
elif resize_p == Resize1440P:
resize_format = 2560
else:
resize_format = 1280
if view_type == HorizontalView:
reduce_rate = safe_div(resize_p, w)
reduce_rate = safe_div(resize_format, 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)
reduce_rate = safe_div(resize_format, h)
new_w = int(float(w) * reduce_rate)
new_h = int(float(h) * reduce_rate)
return new_w, new_h
@ -106,23 +115,32 @@ class S:
os.remove(local_resize_path)
return uo
def proc(self):
def get_wait_deal_images(self):
images = [
{
"src_id": "imgprod/8d/d5/7ddc-1225-4f4c-ace0-c1c63f46b23d"
"src_id": "imgprod/b2/6b/a437-a601-4711-ae60-60a972b57574",
"fmt": "image/png"
}
]
for image in images:
oss_src_id = safe_get_str(image, "src_id")
src_id_python_type = oss_src_id.replace("/", "_")
return images
def proc(self):
images = self.get_wait_deal_images()
idx = 0
for image in images:
idx += 1
# 当前目录
cur_dir = os.getcwd() + "/"
oss_src_id = safe_get_str(image, "src_id")
src_id_python_type = oss_src_id.replace("/", "_")
fmt_ori = safe_get_str(image, "fmt")
fmt = fmt_ori.replace("image/", "")
bulk_reqs = list()
# 先下载到本地,源文件
oss_src_download_path = cur_dir + src_id_python_type + ".jpeg"
# 先下载到本地,源文件 imgprod/a4/98/22b4-391d-445d-834c-5048fb77653c
oss_src_download_path = cur_dir + src_id_python_type + ".{}".format(fmt)
try:
self.save_img_from_oss(oss_src_id, oss_src_download_path)
src_size = os.path.getsize(oss_src_download_path)
@ -140,27 +158,32 @@ class S:
continue
# 720P
uo720 = self.deal_one(oss_src_id, oss_src_download_path, "jpeg", Resize720P)
uo720 = self.deal_one(oss_src_id, oss_src_download_path, fmt, Resize720P)
if not uo720:
logger.Error("deal_one 720 error, oss_src_id: {}".format(oss_src_id))
else:
bulk_reqs.append(uo720)
# 1080P
uo1080 = self.deal_one(oss_src_id, oss_src_download_path, "jpeg", Resize1080P)
uo1080 = self.deal_one(oss_src_id, oss_src_download_path, fmt, Resize1080P)
if not uo1080:
logger.Error("deal_one 1080 error, oss_src_id: {}".format(oss_src_id))
else:
bulk_reqs.append(uo1080)
# 1440P
uo1440 = self.deal_one(oss_src_id, oss_src_download_path, "jpeg", Resize1440P)
uo1440 = self.deal_one(oss_src_id, oss_src_download_path, fmt, Resize1440P)
if not uo1440:
logger.Error("deal_one 1440 error, oss_src_id: {}".format(oss_src_id))
else:
bulk_reqs.append(uo1440)
os.remove(oss_src_download_path)
mongo_ret = self.col_image.bulk_write(bulk_reqs)
if not mongo_ret:
logger.Error("bulk_write fail, oss_src_id: {}".format(oss_src_id))
logger.Info("{}, success, oss_src_id: {}, id: {}".format(idx, oss_src_id, safe_get_int(image, "_id")))
s = S()