feat design 透明和选取透明
fix
This commit is contained in:
@@ -67,7 +67,6 @@ def design(request_data: DesignModel):
|
||||
0
|
||||
],
|
||||
"path": "aida-sys-image/images/female/trousers/0825000630.jpg",
|
||||
"seg_mask_url": "test/result.png",
|
||||
"print": {
|
||||
"element": {
|
||||
"element_angle_list": [],
|
||||
@@ -104,7 +103,6 @@ def design(request_data: DesignModel):
|
||||
0
|
||||
],
|
||||
"path": "aida-sys-image/images/female/blouse/0902003811.jpg",
|
||||
"seg_mask_url": "test/result.png",
|
||||
"print": {
|
||||
"element": {
|
||||
"element_angle_list": [],
|
||||
@@ -141,7 +139,6 @@ def design(request_data: DesignModel):
|
||||
0
|
||||
],
|
||||
"path": "aida-sys-image/images/female/outwear/0825000410.jpg",
|
||||
"seg_mask_url": "test/result.png",
|
||||
"print": {
|
||||
"element": {
|
||||
"element_angle_list": [],
|
||||
|
||||
@@ -20,7 +20,7 @@ class Settings(BaseSettings):
|
||||
|
||||
|
||||
OSS = "minio"
|
||||
DEBUG = False
|
||||
DEBUG = True
|
||||
if DEBUG:
|
||||
LOGS_PATH = "logs/"
|
||||
CATEGORY_PATH = "service/attribute/config/descriptor/category/category_dis.csv"
|
||||
@@ -110,7 +110,7 @@ GI_SYS_IMAGE_URL = "aida-sys-image/generate_image/white_image.jpg"
|
||||
SLOGAN_RABBITMQ_QUEUES = os.getenv("SLOGAN_RABBITMQ_QUEUES", f"Slogan{RABBITMQ_ENV}")
|
||||
|
||||
# Generate Single Logo service config
|
||||
GSL_MODEL_URL = '10.1.1.240:10041'
|
||||
GSL_MODEL_URL = '10.1.1.243:10041'
|
||||
GSL_MINIO_BUCKET = "aida-users"
|
||||
GSL_MODEL_NAME = 'stable_diffusion_xl_transparent'
|
||||
GEN_SINGLE_LOGO_RABBITMQ_QUEUES = os.getenv("GEN_SINGLE_LOGO_RABBITMQ_QUEUES", f"GenSingleLogo{RABBITMQ_ENV}")
|
||||
|
||||
@@ -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
|
||||
@@ -82,13 +82,14 @@ if __name__ == '__main__':
|
||||
# url = "aida-users/89/sketchboard/female/Dress/e6724ab7-8d3f-4677-abe0-c3e42ab7af85.jpeg"
|
||||
# url = "aida-users/87/print/956614a2-7e75-4fbe-9ed0-c1831e37a2c9-4-87.png"
|
||||
# url = "aida-users/89/single_logo/123-89.png"
|
||||
url = "aida-results/result_e961eed6-9278-11ef-a957-0826ae3ad6b3.png"
|
||||
url = "aida-results/result_94d3fc82-a560-11ef-b2c1-0826ae3ad6b3.png"
|
||||
|
||||
# url = "aida-collection-element/12148/Sketchboard/95ea577b-305b-4a62-b30a-39c0dd3ddb3f.png"
|
||||
read_type = "cv2"
|
||||
read_type = "2"
|
||||
if read_type == "cv2":
|
||||
img = oss_get_image(oss_client=minio_client, bucket=url.split('/')[0], object_name=url[url.find('/') + 1:], data_type=read_type)
|
||||
cv2.imshow("", img)
|
||||
cv2.waitKey(0)
|
||||
else:
|
||||
img = oss_get_image(oss_client=minio_client, bucket=url.split('/')[0], object_name=url[url.find('/') + 1:], data_type=read_type)
|
||||
img.show()
|
||||
img.save("原图.png")
|
||||
|
||||
Reference in New Issue
Block a user