feat design 透明和选取透明
fix
This commit is contained in:
@@ -8,9 +8,10 @@ from cv2 import cvtColor, COLOR_BGR2RGBA
|
||||
|
||||
from app.core.config import AIDA_CLOTHING
|
||||
from app.service.design_fast.utils.conversion_image import rgb_to_rgba
|
||||
from app.service.design_fast.utils.transparent import sketch_to_transparent
|
||||
from app.service.design_fast.utils.upload_image import upload_png_mask
|
||||
from app.service.utils.generate_uuid import generate_uuid
|
||||
from app.service.utils.new_oss_client import oss_upload_image
|
||||
from app.service.utils.new_oss_client import oss_upload_image, oss_get_image
|
||||
|
||||
|
||||
class Split(object):
|
||||
@@ -30,6 +31,24 @@ class Split(object):
|
||||
front_mask = cv2.resize(front_mask, new_size)
|
||||
result_front_image[front_mask != 0] = rgba_image[front_mask != 0]
|
||||
result_front_image_pil = Image.fromarray(cvtColor(result_front_image, COLOR_BGR2RGBA))
|
||||
if 'transparent' in result.keys():
|
||||
# 用户自选区域transparent
|
||||
transparent = result['transparent']
|
||||
if transparent['mask_url'] is not None and transparent['mask_url'] != "":
|
||||
# 预处理用户自选区mask
|
||||
seg_mask = oss_get_image(oss_client=self.minio_client, bucket=transparent['mask_url'].split('/')[0], object_name=transparent['mask_url'][transparent['mask_url'].find('/') + 1:], data_type="cv2")
|
||||
seg_mask = cv2.resize(seg_mask, new_size, interpolation=cv2.INTER_NEAREST)
|
||||
# 转换颜色空间为 RGB(OpenCV 默认是 BGR)
|
||||
image_rgb = cv2.cvtColor(seg_mask, cv2.COLOR_BGR2RGB)
|
||||
|
||||
r, g, b = cv2.split(image_rgb)
|
||||
blue_mask = b > r
|
||||
|
||||
# 创建红色和绿色掩码
|
||||
transparent_mask = np.array(blue_mask, dtype=np.uint8) * 255
|
||||
result_front_image_pil = sketch_to_transparent(result_front_image_pil, transparent_mask, transparent["scale"])
|
||||
else:
|
||||
result_front_image_pil = sketch_to_transparent(result_front_image_pil, front_mask, transparent["scale"])
|
||||
result['front_image'], result["front_image_url"], _ = upload_png_mask(self.minio_client, result_front_image_pil, f'{generate_uuid()}', mask=None)
|
||||
|
||||
height, width = front_mask.shape
|
||||
|
||||
26
app/service/design_fast/utils/transparent.py
Normal file
26
app/service/design_fast/utils/transparent.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def sketch_to_transparent(image, mask, transparency):
|
||||
# 打开原始图片
|
||||
image = image.convert("RGBA")
|
||||
# 打开mask图片,假设mask图片是灰度图,白色区域为要处理的区域,黑色区域为保留的区域
|
||||
mask = Image.fromarray(mask)
|
||||
|
||||
# 根据透明度调整因子,将透明度转换为0-255之间的值
|
||||
alpha_value = int((1 - transparency) * 255.0)
|
||||
|
||||
# 获取图片的像素数据
|
||||
image_pixels = image.load()
|
||||
mask_pixels = mask.load()
|
||||
|
||||
width, height = image.size
|
||||
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
# 如果mask区域对应的像素为白色(值大于128,这里假设白色为要处理的区域,可根据实际情况调整)
|
||||
if mask_pixels[x, y] > 128:
|
||||
r, g, b, a = image_pixels[x, y]
|
||||
image_pixels[x, y] = (r, g, b, alpha_value)
|
||||
|
||||
return image
|
||||
Reference in New Issue
Block a user