项目初始化
This commit is contained in:
17
utils/opencv_util.py
Normal file
17
utils/opencv_util.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# 增强对比度 + 灰度转换 + 自适应阈值
|
||||
import cv2
|
||||
|
||||
img = cv2.imread("fb1df265-5085-4bc2-881b-7674b620c4ac.jpg")
|
||||
|
||||
# 转灰度图
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
# 提升对比度
|
||||
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
||||
enhanced = clahe.apply(gray)
|
||||
|
||||
# 二值化处理
|
||||
binary = cv2.adaptiveThreshold(enhanced, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
||||
cv2.THRESH_BINARY_INV, 11, 2)
|
||||
|
||||
cv2.imwrite("processed.png", binary)
|
39
utils/os_utils.py
Normal file
39
utils/os_utils.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import os
|
||||
from fastapi import UploadFile
|
||||
|
||||
|
||||
def file_path(*path):
|
||||
"""
|
||||
拼接返回文件路径
|
||||
:param path:
|
||||
:return:
|
||||
"""
|
||||
return_path = os.path.join(*path)
|
||||
return return_path
|
||||
|
||||
|
||||
def create_folder(*path):
|
||||
"""根据路径创建文件夹"""
|
||||
folder_path = os.path.join(*path)
|
||||
try:
|
||||
os.makedirs(folder_path, exist_ok=True)
|
||||
except Exception as e:
|
||||
print(f"创建文件夹时错误: {e}")
|
||||
return folder_path
|
||||
|
||||
|
||||
def save_images(*path, file: UploadFile):
|
||||
"""
|
||||
保存上传的图片
|
||||
:param path: 路径
|
||||
:param file: 文件
|
||||
:return:
|
||||
"""
|
||||
save_path = os.path.join(*path, file.filename)
|
||||
|
||||
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
||||
with open(save_path, "wb") as f:
|
||||
for line in file.file:
|
||||
f.write(line)
|
||||
return save_path
|
||||
|
11
utils/pillow_util.py
Normal file
11
utils/pillow_util.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from PIL import Image, ImageEnhance
|
||||
|
||||
# 加载图片
|
||||
img = Image.open("fb1df265-5085-4bc2-881b-7674b620c4ac.jpg")
|
||||
|
||||
# 增强对比度(1.5 倍为示例,你可以调节参数)
|
||||
enhancer = ImageEnhance.Contrast(img)
|
||||
img_enhanced = enhancer.enhance(4.0)
|
||||
|
||||
# 保存增强后的图像
|
||||
img_enhanced.save("enhanced_image_pillow.png")
|
Reference in New Issue
Block a user