Skip to content

Commit

Permalink
feat: temp file path (#170)
Browse files Browse the repository at this point in the history
* plugin-jpeg

* save_image

* tempfile

* del
  • Loading branch information
Zeyi-Lin authored Sep 25, 2024
1 parent f262374 commit 5959376
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 63 deletions.
Empty file removed demo/kb_output/.gitkeep
Empty file.
8 changes: 4 additions & 4 deletions demo/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,22 +670,22 @@
"plugin": {
"en": {
"label": "🤖Plugin",
"choices": ["Face Alignment", "Layout Photo Crop Line"],
"choices": ["Face Alignment", "Layout Photo Crop Line", "JPEG Format"],
"value": ["Layout Photo Crop Line"]
},
"zh": {
"label": "🤖插件",
"choices": ["人脸旋转对齐", "排版照裁剪线"],
"choices": ["人脸旋转对齐", "排版照裁剪线", "JPEG格式"],
"value": ["排版照裁剪线"]
},
"ja": {
"label": "🤖プラグイン",
"choices": ["顔の整列", "レイアウト写真の切り取り線"],
"choices": ["顔の整列", "レイアウト写真の切り取り線", "JPEGフォーマット"],
"value": ["レイアウト写真の切り取り線"]
},
"ko": {
"label": "🤖플러그인",
"choices": ["얼굴 정렬", "레이아웃 사진 자르기 선"],
"choices": ["얼굴 정렬", "레이아웃 사진 자르기 선", "JPEG 포맷"],
"value": ["레이아웃 사진 자르기 선"]
},
},
Expand Down
129 changes: 70 additions & 59 deletions demo/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ def process(
layout_photo_crop_line_option = True
else:
layout_photo_crop_line_option = False
if LOCALES["plugin"][language]["choices"][2] in plugin_option:
jpeg_format_option = True
else:
jpeg_format_option = False

idphoto_json = self._initialize_idphoto_json(
mode_option, color_option, render_option_index, image_kb_options, layout_photo_crop_line_option
mode_option, color_option, render_option_index, image_kb_options, layout_photo_crop_line_option, jpeg_format_option
)

# 处理尺寸模式
Expand Down Expand Up @@ -164,6 +168,7 @@ def _initialize_idphoto_json(
render_option,
image_kb_options,
layout_photo_crop_line_option,
jpeg_format_option,
):
"""初始化idphoto_json字典"""
return {
Expand All @@ -174,6 +179,7 @@ def _initialize_idphoto_json(
"custom_image_kb": None,
"custom_image_dpi": None,
"layout_photo_crop_line_option": layout_photo_crop_line_option,
"jpeg_format_option": jpeg_format_option,
}

# 处理尺寸模式
Expand Down Expand Up @@ -362,45 +368,27 @@ def _process_generated_photo(
)

# 调整图片大小
output_image_path_dict = self._resize_image_if_needed(
output_image_path_dict = self._save_image(
result_image_standard,
result_image_hd,
result_image_layout,
idphoto_json,
format="jpeg" if idphoto_json["jpeg_format_option"] else "png",
)

# 返回
if result_image_layout is not None:
result_image_layout = output_image_path_dict["layout"]["path"]

return self._create_response(
output_image_path_dict["standard"]["path"],
output_image_path_dict["hd"]["path"],
result_image_standard_png,
result_image_hd_png,
gr.update(value=result_image_layout, visible=result_image_layout_visible),
gr.update(value=result_image_template, visible=result_image_template_visible),
gr.update(visible = result_image_template_visible),
)

# 如果output_image_path_dict为None,即没有设置KB和DPI
if output_image_path_dict is None:
return self._create_response(
result_image_standard,
result_image_hd,
result_image_standard_png,
result_image_hd_png,
gr.update(value=result_image_layout, visible=result_image_layout_visible),
gr.update(value=result_image_template, visible=result_image_template_visible),
gr.update(visible = result_image_template_visible),
)
# 如果output_image_path_dict不为None,即设置了KB和DPI
else:
if output_image_path_dict["layout"]["processed"]:
result_image_layout = output_image_path_dict["layout"]["path"]
return self._create_response(
(
output_image_path_dict["standard"]["path"]
if output_image_path_dict["standard"]["processed"]
else result_image_standard
),
(
output_image_path_dict["hd"]["path"]
if output_image_path_dict["hd"]["processed"]
else result_image_hd
),
result_image_standard_png,
result_image_hd_png,
gr.update(value=result_image_layout, visible=result_image_layout_visible),
gr.update(value=result_image_template, visible=result_image_template_visible),
gr.update(visible = result_image_template_visible),
)

# 渲染背景
def _render_background(self, result_image_standard, result_image_hd, idphoto_json, language):
Expand Down Expand Up @@ -512,19 +500,17 @@ def _add_watermark(
result_image_hd = add_watermark(image=result_image_hd, **watermark_params)
return result_image_standard, result_image_hd

def _resize_image_if_needed(
def _save_image(
self,
result_image_standard,
result_image_hd,
result_image_layout,
idphoto_json,
format="png",
):
"""如果需要,调整图片大小"""
# 设置输出路径
base_path = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "demo/kb_output"
)
# 设置输出路径(临时目录)
import tempfile
base_path = tempfile.mkdtemp()
timestamp = int(time.time())
output_paths = {
"standard": {
Expand Down Expand Up @@ -556,53 +542,78 @@ def _resize_image_if_needed(
custom_kb,
dpi=custom_dpi,
)
output_paths["standard"]["processed"] = True
# 保存高清图像和排版图像
save_image_dpi_to_bytes(
result_image_hd, output_paths["hd"]["path"], dpi=custom_dpi
)
output_paths["hd"]["processed"] = True
if result_image_layout is not None:
save_image_dpi_to_bytes(
result_image_layout, output_paths["layout"]["path"], dpi=custom_dpi
)
output_paths["layout"]["processed"] = True

return output_paths

# 只有自定义DPI的情况
elif custom_dpi:
for key in output_paths:
output_paths[key]["path"] += f"_{custom_dpi}dpi.{format}"
# 保存所有图像,使用自定义DPI
# 如果只换底,则不保存排版图像
if key == "layout" and result_image_layout is None:
pass
else:
save_image_dpi_to_bytes(
locals()[f"result_image_{key}"],
output_paths[key]["path"],
dpi=custom_dpi,
)
output_paths[key]["processed"] = True
continue
output_paths[key]["path"] += f"_{custom_dpi}dpi.{format}"
save_image_dpi_to_bytes(
locals()[f"result_image_{key}"],
output_paths[key]["path"],
dpi=custom_dpi,
)

return output_paths

# 只有自定义KB的情况
elif custom_kb:
output_paths["standard"]["path"] += f"_{custom_kb}kb.{format}"
# 只调整标准图像大小并保存
output_paths["hd"]["path"] += f".{format}"
if not (key == "layout" and result_image_layout is None):
output_paths[key]["path"] += f".{format}"

# 只调整标准图像大小
resize_image_to_kb(
result_image_standard,
output_paths["standard"]["path"],
custom_kb,
dpi=300,
)
output_paths["standard"]["processed"] = True

# 保存高清图像和排版图像
save_image_dpi_to_bytes(
result_image_hd, output_paths["hd"]["path"], dpi=300
)
if result_image_layout is not None:
save_image_dpi_to_bytes(
result_image_layout, output_paths["layout"]["path"], dpi=300
)

return output_paths

# 如果没有自定义设置,返回None
return None
# 没有自定义设置
else:
output_paths["standard"]["path"] += f".{format}"
output_paths["hd"]["path"] += f".{format}"
output_paths["layout"]["path"] += f".{format}"

# 保存所有图像
save_image_dpi_to_bytes(
result_image_standard, output_paths["standard"]["path"], dpi=300
)
save_image_dpi_to_bytes(
result_image_hd, output_paths["hd"]["path"], dpi=300
)
if result_image_layout is not None:
save_image_dpi_to_bytes(
result_image_layout, output_paths["layout"]["path"], dpi=300
)

return output_paths


def _create_response(
self,
Expand All @@ -613,7 +624,7 @@ def _create_response(
result_layout_image_gr,
result_image_template_gr,
result_image_template_accordion_gr,
):
):
"""创建响应"""
response = [
result_image_standard,
Expand Down

0 comments on commit 5959376

Please sign in to comment.