You need to enable JavaScript to run this app.
文档中心
图像生成大模型

图像生成大模型

复制全文
下载 pdf
最佳实践
智能分割最佳实践
复制全文
下载 pdf
智能分割最佳实践
效果图​
​
原图​
智能分割​
前景图​
图层合并​
​图片​​
​图片​​
​图片​​
​图片​​
​
Mask转前景图Demo​
​
# mask转前景图​
import cv2​
from PIL import Image​
import numpy as np​
import time​
​
​
class UnsupportedFormat(Exception):​
def __init__(self, input_type):​
self.t = input_type​
​
def __str__(self):​
return "不支持'{}'模式的转换,请使用为图片地址(path)、PIL.Image(pil)或 OpenCV(cv2)模式".format(self.t)​
​
​
class MatteMatting():​
def __init__(self, original_graph, mask_graph, input_type='path'):​
"""​
将输入的图片经过蒙版转化为透明图构造函数​
:param original_graph:输入的图片地址、PIL格式、CV2 格式​
:param mask_graph:蒙版的图片地址、PIL格式、CV2 格式​
:param input_type:输入的类型,有 path:图片地址、pil:pil 类型、cv2 类型​
"""​
if input_type == 'path':​
self.img1 = cv2.imread(original_graph)​
self.img2 = cv2.imread(mask_graph)​
elif input_type == 'pil':​
self.img1 = self.__image_to_opencv(original_graph)​
self.img2 = self.__image_to_opencv(mask_graph)​
elif input_type == 'cv2':​
self.img1 = original_graph​
self.img2 = mask_graph​
else:​
raise UnsupportedFormat(input_type)​
​
@staticmethod​
def __transparent_back(img):​
"""​
:param img: 传入图片地址​
:return: 返回替换白色后的透明图​
"""​
img = img.convert('RGBA')​
L, H = img.size​
color_0 = (255, 255, 255, 255) # 要替换的颜色​
for h in range(H):​
for l in range(L):​
dot = (l, h)​
color_1 = img.getpixel(dot)​
if color_1 == color_0:​
color_1 = color_1[:-1] + (0,)​
img.putpixel(dot, color_1)​
return img​
​
def save_image(self, mask_flip=False):​
"""​
用于保存透明图​
:param mask_flip: 蒙版翻转,将蒙版的黑白颜色翻转;True 翻转;False 不使用翻转​
"""​
timestamp = str(int(time.time()))​
if mask_flip:​
img2 = cv2.bitwise_not(self.img2) # 黑白翻转​
image = cv2.add(self.img1, img2)​
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # OpenCV 转换成 PIL.Image 格式​
img = self.__transparent_back(image)​
img.save(f"<YOUR_IMAGE_URL>{timestamp}.png")​
​
@staticmethod​
def __image_to_opencv(image):​
"""​
PIL.Image 转换成 OpenCV 格式​
"""​
img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)​
return img​
​
​
if __name__ == '__main__':​
mm = MatteMatting("<YOUR_IMAGE_PATH>",​
"<YOUR_IMAGE_PATH>")​
mm.save_image(mask_flip=True) # mask_flip是指蒙版翻转,即把白色的变成黑色的,黑色的变成白色的​
​
获取最大实体Mask逻辑​
"return_format": 1时返回的第一个​
多个实体合并逻辑​
​
import cv2​
from PIL import Image​
import os​
​
# 定义根目录,这里存放要处理的文件​
root_dir = '<YOUR_IMAGE_PATH>'​
# 获取根目录下的所有文件列表​
files = os.listdir(root_dir)​
​
# 生成包含'mask.jpeg'和其对应的'mask_1.jpeg'的文件对列表​
pairs = [(file, file.replace('.jpeg', '_1.jpeg', )) for file in files if file.endswith('mask.jpeg')]​
​
for pair in pairs:​
# 读取'mask.jpeg'文件​
mask0 = cv2.imread(os.path.join(root_dir, pair[0]))​
# 读取'mask_1.jpeg'文件​
mask1 = cv2.imread(os.path.join(root_dir, pair[1]))​
if mask1 is None: # 如果'mask_1.jpeg'不存在,则跳过本次循环​
continue​
# 将两个图像进行叠加​
image = mask0 + mask1​
# 定义输出文件名,是'mask.jpeg'替换为'_combined.jpeg'​
output_file = pair[0].replace('.jpeg', '_combined.jpeg')​
​
# 保存合并后的图片​
cv2.imwrite(output_file, image)​
​
完整入参示例​
​
{​
"image_urls": [​
"<YOUR_IMAGE_URL>"],​
"req_key": "entity_seg",​
"return_url": true,​
"max_entity": 20,​
"return_format": 1,​
"refine_mask": 0​
}​
​
完整返回示例​
​
{​
"code": 10000,​
"data": {​
"algorithm_base_resp": {​
"status_code": 0,​
"status_message": "Success"​
},​
"binary_data_base64": [],​
"entity_num": [​
3​
],​
"image_urls": ["https:xxx ",​
"https:xxx",​
"https:xxx"​
],​
"ori_height": [1706],​
"ori_width": [960],​
"seg_score": [0.999703049659729, 0.9998470544815063, 0.9993122220039368]​
},​
"message": "Success",​
"request_id": "202407231631029A865ECF357FC3D0CD9F",​
"status": 10000,​
"time_elapsed": "2.366757385s"​
}​
​
​
最近更新时间:2026.08.24 11:22:21
这个页面对您有帮助吗?
有用
有用
无用
无用