Skip to content

Commit

Permalink
feat: 新增图片格式转换工具,优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
pangxiaobin committed Jul 31, 2024
1 parent b816844 commit 9be8076
Show file tree
Hide file tree
Showing 54 changed files with 471 additions and 200 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
## 项目介绍

> 本地模型算法进行抠图,支持单张和批量抠图
> 支持单张抠图和批量抠图
> 支持拖拽和粘贴
> AI证件照抠图功能
- 本地模型算法进行抠图,支持单张和批量抠图
- 支持单张抠图和批量抠图
- 支持拖拽和粘贴
- AI证件照抠图功能
- 支持图片格式转换
- 语言支持中文和英文
- 支持暗色和亮色主题
- 项目开源,可供学习和参考

## 打包后的运行文件

Expand All @@ -19,9 +23,13 @@

![运行截图](./imgs/1.png)
![运行截图](./imgs/2.png)
![运行截图](./imgs/3.png)
![运行截图](./imgs/4.png)
![运行截图](./imgs/8.png)
![运行截图](./imgs/9.png)
![运行截图](./imgs/10.png)
![运行截图](./imgs/11.png)
![运行截图](./imgs/12.png)
![运行截图](./imgs/3.png)
![运行截图](./imgs/5.png)
![运行截图](./imgs/6.png)
![运行截图](./imgs/8.png)
![运行截图](./imgs/7.GIF)
8 changes: 8 additions & 0 deletions backend/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from utilities.log import logger
from utilities.response import res200, res400, res500
from conf.setting import settings
from utilities.utils import img_to_base64


class API:
Expand Down Expand Up @@ -46,3 +47,10 @@ def get_system_info(self, playload) -> dict:
"email": settings.EMAIL,
}
)

def get_local_file_base64(self, file):
try:
img = img_to_base64(file)
return res200(data={"base64_image": img})
except Exception as e:
return res500(f"Error in get_local_file_base64: {e}")
7 changes: 0 additions & 7 deletions backend/api/ai_matting.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ def get_folder_images(self, folder_path: str) -> dict:
logger.error(f"Error in get_folder_images: {e} {folder_path}")
return res500("Error in get_folder_images")

def get_local_file_base64(self, file):
try:
img = img_to_base64(file)
return res200(data={"base64_image": img})
except Exception as e:
return res500(f"Error in get_local_file_base64: {e}")

def predict_from_folder_img(self, playload: dict) -> dict:
"""
Predict the no-background image of the input image.
Expand Down
28 changes: 28 additions & 0 deletions backend/api/convert_image_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from utilities.response import res500, res200, res400
from utilities.utils import convert_image_format
from conf.setting import settings
from webview import SAVE_DIALOG, OPEN_DIALOG
import webview
import time


class ConvertImageAPI:
name = "convert_image"

def get_convert_image(self, playload):
try:
input_path = playload["input_path"]
output_format = playload["output_format"]
except KeyError:
return res500("Invalid input")
filename = f"[{settings.TOOL_NAME}]-{int(time.time()*1000)}-output.{output_format.lower()}"
result = self.open_save_dialog(
filename,
)
if not result:
return res200(msg="Cancel")
flag = convert_image_format(input_path, output_format, result)
if flag:
return res200(msg="Success")
else:
return res400(msg="Cancel")
2 changes: 1 addition & 1 deletion backend/conf/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Settings(BaseSettings):

BASE_DIR: Path = Field(default=BASE_DIR, description="项目基础路径")

VERSION: str = Field(default="0.1.1", description="版本号")
VERSION: str = Field(default="0.1.2", description="版本号")
# 日志
LOG: dict = {"max_log_size": "20MB", "backup_count": 20}

Expand Down
16 changes: 11 additions & 5 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import time
from api.user_api import SettingAPI
from api.ai_matting import AIMattingAPI
from api.convert_image_api import ConvertImageAPI
import platform
import subprocess
import os
import traceback


def main():
Expand Down Expand Up @@ -61,6 +63,13 @@ def save_png_dialog(
return res500(f"save_png_dialog error: {e}")
return res200({"file_path": result})

def open_save_dialog(self, save_filename):
result = window.create_file_dialog(
webview.SAVE_DIALOG,
save_filename=save_filename,
)
return result

def save_png_add_bg_dialog(
self,
playload,
Expand Down Expand Up @@ -105,10 +114,11 @@ def open_and_select_file(self, file_path):
API.save_png_dialog = save_png_dialog
API.open_and_select_file = open_and_select_file
API.save_png_add_bg_dialog = save_png_add_bg_dialog
API.open_save_dialog = open_save_dialog

api = API()

api_class_list = [SettingAPI, AIMattingAPI]
api_class_list = [SettingAPI, AIMattingAPI, ConvertImageAPI]
for api_class in api_class_list:
api.add_apis(api_class)
try:
Expand All @@ -124,8 +134,6 @@ def open_and_select_file(self, file_path):
confirm_close=True,
)
except Exception as e:
import traceback

traceback.print_exc()

logger.info(f"Debug: {settings.DEBUG}")
Expand Down Expand Up @@ -153,8 +161,6 @@ def bind(window):
try:
webview.start(bind, window, debug=settings.DEBUG, http_server=True)
except:
import traceback

traceback.print_exc()
logger.error("Error: webview.start() failed")

Expand Down
2 changes: 1 addition & 1 deletion backend/utilities/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def JsonResponse(data):
return json.dumps(data)


def res200(data, msg="success"):
def res200(data=None, msg="success"):
return JsonResponse({"code": 200, "msg": msg, "data": data})


Expand Down
35 changes: 35 additions & 0 deletions backend/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,38 @@ def save_bs64_image_add_bg(base64_data, hex_color, save_path):
background_image.save(save_path, "PNG")

return save_path


def convert_image_format(input_path, output_format, output_path=None):
try:
# 打开原始图片
image = Image.open(input_path)

# 确保输出格式合法并转换格式
if output_format.lower() == "jpg" or output_format.lower() == "jpeg":
image = image.convert("RGB") # 转换为RGB格式
image.save(output_path, "JPEG")
elif output_format.lower() == "png":
image.save(output_path, "PNG")
elif output_format.lower() == "gif":
image.save(output_path, "GIF")
elif output_format.lower() == "bmp":
image.save(output_path, "BMP")
elif output_format.lower() == "ico":
image.save(output_path, "ICO")
elif output_format.lower() == "icns":
image.save(output_path, "ICNS")
elif output_format.lower() == "webp":
image.save(output_path, "WEBP")
elif output_format.lower() == "pdf":
image.save(output_path, "PDF")
elif output_format.lower() == "tiff":
image.save(output_path, "TIFF")
else:
return False
return True
except Exception as e:
import traceback

print(traceback.format_exc())
return False

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/web/assets/AIPhotoEditor-BowqQexr.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/web/assets/AIPhotoEditor-lJ1ihCKx.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.container{display:flex;flex-direction:column;align-items:center;padding:2rem}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9be8076

Please sign in to comment.