| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 |
- import copy
- import json
- import os
- from PIL import Image
- from alibabacloud_imageseg20191230.client import Client as imageseg20191230Client
- from alibabacloud_imageseg20191230.models import SegmentCommodityAdvanceRequest
- from alibabacloud_imageseg20191230 import models as imageseg_20191230_models
- from alibabacloud_tea_util.models import RuntimeOptions
- from alibabacloud_tea_openapi import models as open_api_models
- from alibabacloud_tea_openapi.models import Config
- from alibabacloud_tea_util import models as util_models
- import requests
- from io import BytesIO
- import cv2
- import numpy as np
- from func_timeout import func_set_timeout
- from func_timeout import FunctionTimedOut
- from .multi_threaded_image_saving import ImageSaver
- import settings, time, math
- from logger import logger
- # 自己的
- AccessKeyId = "LTAI5tCk4p881X8hymj2FYFk"
- AccessKeySecret = "yBYIYzX8CL24r5ZgEx2AgZyDBmFkIK"
- def uploadImage(im: Image) -> str:
- img_byte_io = BytesIO()
- # 根据图片模式选择保存格式
- if im.mode == 'RGBA':
- im.save(img_byte_io, format='PNG')
- else:
- im.save(img_byte_io, format='JPEG')
- img_byte_io.seek(0) # 重置指针到开头
- post_headers = {"Authorization": settings.USER_TOKEN}
- url = settings.DOMAIN + "/api/upload"
- # 使用字节流上传
- resultData = requests.post(
- url,
- files={"file": ("image.jpg", img_byte_io, "image/jpeg")},
- headers=post_headers
- ).json()
- return resultData["data"]["url"]
- # 惠利玛公司的KEY
- # AccessKeyId = 'LTAI5tCk4p881X8hymj2FYFk'
- # AccessKeySecret = 'rQMgHwciTN4Gusbpt8CM8tflgsxh1V'
- # https://help.aliyun.com/zh/viapi/developer-reference/python?spm=a2c4g.11186623.0.i0#task-2252575
- # pip install alibabacloud_goodstech20191230
- # pip install alibabacloud_tea_openapi
- # pip install alibabacloud_tea_util
- class Segment(object):
- def __init__(self):
- self.client = self.create_client()
- def get_no_bg_common(self, file_path):
- # 初始化RuntimeObject
- runtime_option = RuntimeOptions()
- try:
- # 场景一:文件在本地
- img = open(file_path, 'rb')
- # 使用完成之后记得调用img.close()关闭流
- # 场景二,使用任意可访问的url
- # url = 'https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeBankCard/yhk1.jpg'
- # img = io.BytesIO(urlopen(url).read())
- # 4、初始化Request,这里只是以RecognizeBankCard为例,其他能力请使用相应能力对应的类
- request = SegmentCommodityAdvanceRequest()
- request.image_urlobject = img
- # 5、调用api,注意,recognize_bank_card_advance需要更换为相应能力对应的方法名。方法名是根据能力名称按照一定规范形成的,如能力名称为SegmentCommonImage,对应方法名应该为segment_common_image_advance。
- response = self.client.segment_common_image_advance(request, runtime_option)
- # 获取整体结果
- # print(response.body)
- img.close()
- return response.body
- # 获取单个字段,这里只是一个例子,具体能力下的字段需要看具体能力的文档
- # print(response.body.data.card_number)
- # tips: 可通过response.body.__dict__查看属性名称
- except Exception as error:
- # 获取整体报错信息
- print("error", error)
- return None
- # 获取单个字段
- # print(error.code)
- # tips: 可通过error.__dict__查看属性名称
- def get_image_orientation(self, img):
- # 获取EXIF数据
- exif = None
- try:
- if hasattr(img, 'getexif'):
- exif_data = img.getexif()
- if exif_data:
- exif = dict(exif_data)
- elif hasattr(img, '_getexif'):
- exif = img._getexif()
- except Exception:
- print("获取EXIF数据失败", img)
- if exif is not None:
- # EXIF标签274对应的是Orientation
- orientation = exif.get(0x0112)
- print("orientation", orientation)
- if orientation == 2:
- # 水平翻转
- img = img.transpose(Image.FLIP_LEFT_RIGHT)
- elif orientation == 3:
- # 旋转180度
- img = img.rotate(180, expand=True)
- elif orientation == 4:
- # 垂直翻转
- img = img.transpose(Image.FLIP_TOP_BOTTOM)
- elif orientation == 5:
- # 水平翻转后顺时针旋转90度
- img = img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
- elif orientation == 6:
- # 顺时针旋转90度
- img = img.transpose(Image.ROTATE_270)
- elif orientation == 7:
- # 水平翻转后逆时针旋转90度
- img = img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90)
- elif orientation == 8:
- # 逆时针旋转90度
- img = img.transpose(Image.ROTATE_90)
- else:
- print("没有EXIF数据或没有方向信息")
- orientation = 1
- return img
- def get_bo_bg_goods_ultra_background(self, im, is_shadow=False, api_url=None):
- imageUrl = uploadImage(im)
- # imageUrl = imageUrl + "?x-oss-process=image/auto-orient,1"
- if not settings.USER_TOKEN:
- print("错误:USER_TOKEN 未配置或为空")
- return None
- print("图片上传成功", imageUrl)
- post_headers = {"Authorization": "Bearer " + settings.USER_TOKEN}
- data = {
- "image_url": imageUrl,
- "is_shadow": is_shadow
- }
- response = requests.post(
- api_url, json=data, headers=post_headers
- )
- print(f"响应状态码: {response.status_code}")
- print(f"响应内容: {response.text[:500]}") # 只打印前500字符
- resultData = response.json()
- print("旗舰版抠图请求", resultData)
- # 安全地获取返回值
- data = resultData.get("data")
- if data is None:
- print("旗舰版抠图返回的data为空")
- return None
- cutout_image = data.get("cutout_image")
- if cutout_image is None:
- print("旗舰版抠图返回的cutout_image为空")
- return None
- return cutout_image
- def get_ultra_shadow(self, original_image, cutout_image, opacity, bright_target, api_url=None):
- image_original_url = uploadImage(original_image)
- image_cutcou_url = uploadImage(cutout_image)
- # imageUrl = imageUrl + "?x-oss-process=image/auto-orient,1"
- if not settings.USER_TOKEN:
- print("错误:USER_TOKEN 未配置或为空")
- return None
- print("阴影图处理,图片上传成功", image_original_url)
- post_headers = {"Authorization": "Bearer " + settings.USER_TOKEN}
- data = {
- "oraginal_image": image_original_url,
- "cutout_image": image_cutcou_url,
- "opacity": opacity,
- "bright_target": bright_target
- }
- print("阴影处理参数:", data)
- response = requests.post(
- api_url, json=data, headers=post_headers
- )
- print(f"响应状态码: {response.status_code}")
- print(f"响应内容: {response.text[:500]}") # 只打印前500字符
- resultData = response.json()
- print("旗舰版抠图请求", resultData)
- # 安全地获取返回值
- data = resultData.get("data")
- if data is None:
- print("旗舰版抠图返回的data为空")
- return None
- matte_image = data.get("matte_image")
- if matte_image is None:
- print("旗舰版 阴影处理 返回的 matte_image为空")
- return None
- return matte_image
- def get_no_bg_goods(self, file_path=None, _im=None):
- # https://blog.csdn.net/weixin_43411585/article/details/107780941
- im = _im
- # im.save(file_path)
- img = BytesIO()
- im.save(img, format='JPEG') # format: PNG or JPEG
- img.seek(0) # rewind to the start
- request = imageseg_20191230_models.SegmentCommodityAdvanceRequest()
- request.image_urlobject = img
- client = self.create_client()
- # 5、调用api,注意,recognize_bank_card_advance需要更换为相应能力对应的方法名。方法名是根据能力名称按照一定规范形成的,如能力名称为SegmentCommonImage,对应方法名应该为segment_common_image_advance。
- runtime = util_models.RuntimeOptions()
- response = client.segment_commodity_advance(request, runtime)
- # img.close()
- # print("1111111111111", response.body)
- return response.body
- def create_client(self):
- """
- 使用AK&SK初始化账号Client
- @param access_key_id:
- @param access_key_secret:
- @return: Client
- @throws Exception
- """
- config = open_api_models.Config(
- # 必填,您的 AccessKey ID,
- access_key_id=AccessKeyId,
- # 必填,您的 AccessKey Secret,
- access_key_secret=AccessKeySecret
- )
- # 访问的域名
- config.endpoint = f'imageseg.cn-shanghai.aliyuncs.com'
- return imageseg20191230Client(config)
- class Picture:
- def __init__(self, in_path, im=None):
- if im:
- self.im = im
- else:
- self.im = Image.open(in_path)
- self.x, self.y = self.im.size
- # print(self.x, self.y)
- def save_img(self, outpath, quality=90):
- # self.im = self.im.convert("RGB")
- self.im.save(outpath, quality=quality)
- def resize(self, width):
- re_x = int(width)
- re_y = int(self.y * re_x / self.x)
- self.im = self.im.resize((re_x, re_y), Image.BICUBIC)
- self.x, self.y = self.im.size
- def resize_by_heigh(self, heigh):
- re_y = int(heigh)
- re_x = int(self.x * re_y / self.y)
- self.im = self.im.resize((re_x, re_y), Image.BICUBIC)
- self.x, self.y = self.im.size
- class RemoveBgALi(object):
- def __init__(self):
- self.saver = ImageSaver()
- self.segment = Segment()
- @func_set_timeout(40)
- def get_image_cut_new(self, file_path, out_file_path=None, original_im=None):
- if original_im:
- original_pic = Picture(in_path=None, im=original_im)
- else:
- original_pic = Picture(file_path)
- if original_pic.im.mode != "RGB":
- original_pic.im = original_pic.im.convert("RGB")
- new_pic = copy.copy(original_pic)
- after_need_resize = False
- if new_pic.x > new_pic.y:
- if new_pic.x > 2000:
- after_need_resize = True
- new_pic.resize(2000)
- else:
- if new_pic.y > 2000:
- after_need_resize = True
- new_pic.resize_by_heigh(heigh=2000)
- # new_pic.im.show()
- body = self.segment.get_no_bg_goods(file_path=None, _im=new_pic.im)
- body = eval(str(body))
- try:
- image_url = body["Data"]["ImageURL"]
- except BaseException as e:
- print("阿里抠图错误:", e)
- # 处理失败,需要删除过程图片
- return None
- # 字节流转PIL对象
- response = requests.get(image_url)
- pic = response.content
- _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
- box_size = _img_im.getbbox()
- new_pp4_im = _img_im.crop(box_size)
- byte_io = BytesIO()
- new_pp4_im.save(byte_io, format='PNG') # 将图像保存为 PNG 格式到 BytesIO 对象
- byte_io.seek(0) # 将指针重置到流的开头,以便后续读取
- return byte_io
- @func_set_timeout(40)
- def get_image_cut(self, file_path, out_file_path=None, original_im=None):
- if original_im:
- original_pic = Picture(in_path=None, im=original_im)
- else:
- original_pic = Picture(file_path)
- if original_pic.im.mode != "RGB":
- print("抠图图片不能是PNG")
- return None
- new_pic = copy.copy(original_pic)
- after_need_resize = False
- if new_pic.x > new_pic.y:
- if new_pic.x > 2000:
- after_need_resize = True
- new_pic.resize(2000)
- else:
- if new_pic.y > 2000:
- after_need_resize = True
- new_pic.resize_by_heigh(heigh=2000)
- # new_pic.im.show()
- body = self.segment.get_no_bg_goods(file_path=None, _im=new_pic.im)
- body = eval(str(body))
- try:
- image_url = body["Data"]["ImageURL"]
- except BaseException as e:
- print("阿里抠图错误:", e)
- # 处理失败,需要删除过程图片
- return None
- # 字节流转PIL对象
- response = requests.get(image_url)
- pic = response.content
- _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
- # 原图更大,则需要执行CV处理
- if after_need_resize:
- # 将抠图结果转成mask
- # _img_im = Image.open(_path)
- # 将抠图结果放大到原始图大小
- _img_im = _img_im.resize(original_pic.im.size)
- new_big_mask = Image.new('RGB', _img_im.size, (0, 0, 0))
- white = Image.new('RGB', _img_im.size, (255, 255, 255))
- new_big_mask.paste(white, mask=_img_im.split()[3])
- # ---------制作选区缩小的mask
- # mask = cv2.imread(mask_path)
- # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
- mask = cv2.cvtColor(np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY) # 将PIL 格式转换为 CV对象
- mask[mask != 255] = 0
- # 黑白反转
- # mask = 255 - mask
- # 选区缩小10
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
- erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
- # -------再进行抠图处理
- mask = Image.fromarray(cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)) # CV 对象转 PIL
- transparent_im = Image.new('RGBA', original_pic.im.size, (0, 0, 0, 0))
- # original_pic.im.show()
- # mask.show()
- transparent_im.paste(original_pic.im, (0, 0), mask.convert('L'))
- # transparent_im.show()
- # 上述抠图结果进行拼接
- _img_im.paste(transparent_im, (0, 0), transparent_im)
- # _img_im.show("11111111111111111111111")
- if out_file_path:
- self.saver.save_image(
- image=_img_im, file_path=out_file_path,
- quality=100, dpi=(350, 350), _format="PNG"
- )
- # _img_im.save(out_file_path)
- return _img_im
- def get_image_cut1(self, file_path, out_file_path=None):
- original_pic = Picture(file_path)
- new_pic = copy.copy(original_pic)
- if new_pic.x > 2000:
- new_pic.resize(2000)
- # new_pic.im.show()
- body = self.segment.get_no_bg_goods(file_path=out_file_path, _im=new_pic.im)
- body = eval(str(body))
- try:
- image_url = body["Data"]["ImageURL"]
- except BaseException as e:
- print("阿里抠图错误:", e)
- # 处理失败,需要删除过程图片
- return None
- # 字节流转PIL对象
- response = requests.get(image_url)
- pic = response.content
- _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
- if original_pic.x > 2000:
- # 原图更大,则需要执行CV处理
- # _img_im.show()
- # 对mask进行放大,然后进行抠图处理
- print("对mask进行放大,然后进行抠图处理")
- transparent_im = Image.new('RGBA', original_pic.im.size, (0, 0, 0, 0))
- # original_pic.im.show()
- # mask.show()
- _img_im = _img_im.resize((original_pic.x, original_pic.y))
- # _img_im.show()
- transparent_im.paste(original_pic.im, (0, 0), mask=_img_im)
- # transparent_im.show()
- # transparent_im.show()
- _img_im = transparent_im
- # 上述抠图结果进行拼接
- # _img_im.paste(transparent_im, (0, 0), transparent_im)
- pass
- _img_im.save(out_file_path)
- return _img_im
- def download_picture(self, url, out_path):
- response = requests.get(url)
- pic = response.content
- with open(out_path, 'wb') as f:
- f.write(pic)
- class RemoveUltraBackground:
- def __init__(self):
- self.api_url = '/api/ai_image/segment_shadow/segment_service'
- self.headers = {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json'
- }
- self.saver = ImageSaver()
- self.segment = Segment()
- self.r_ali = RemoveBgALi()
- self.logger = logger
- @func_set_timeout(40)
- def get_image_cut_ultra(self, out_file_path=None, im_image=None):
- original_pic = Picture(in_path=None, im=im_image)
- original_pic.im = self.segment.get_image_orientation(im_image)
- original_pic.x, original_pic.y = original_pic.im.size
- if original_pic.im.mode != "RGB":
- print("抠图图片不能是PNG")
- return None
- new_pic = copy.copy(original_pic)
- # after_need_resize = False
- # if new_pic.x > new_pic.y:
- # if new_pic.x > 2000:
- # after_need_resize = True
- # new_pic.resize(2000)
- # else:
- # if new_pic.y > 2000:
- # after_need_resize = True
- # new_pic.resize_by_heigh(heigh=2000)
- print("使用旗舰版抠图")
- try:
- api_url = f"{settings.DOMAIN}{self.api_url}"
- image_url = self.segment.get_bo_bg_goods_ultra_background(im=new_pic.im, api_url=api_url)
- except BaseException as e:
- print("旗舰版抠图异常:", e)
- # 处理失败,需要删除过程图片
- return None
- if image_url is None:
- return None
- # 字节流转PIL对象
- print("image_url", image_url)
- response = requests.get(image_url)
- pic = response.content
- _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
- # # 原图更大,则需要执行CV处理
- # if after_need_resize:
- # # 将抠图结果转成mask
- # # _img_im = Image.open(_path)
- # # 将抠图结果放大到原始图大小
- # _img_im = _img_im.resize(original_pic.im.size)
- # new_big_mask = Image.new('RGB', _img_im.size, (0, 0, 0))
- # white = Image.new('RGB', _img_im.size, (255, 255, 255))
- # new_big_mask.paste(white, mask=_img_im.split()[3])
- #
- # # ---------制作选区缩小的mask
- # # mask = cv2.imread(mask_path)
- # # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
- # mask = cv2.cvtColor(np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY) # 将PIL 格式转换为 CV对象
- # mask[mask != 255] = 0
- # # 黑白反转
- # # mask = 255 - mask
- # # 选区缩小10
- # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
- # erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
- #
- # # -------再进行抠图处理
- # mask = Image.fromarray(cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)) # CV 对象转 PIL
- # transparent_im = Image.new('RGBA', original_pic.im.size, (0, 0, 0, 0))
- # # original_pic.im.show()
- # # mask.show()
- # transparent_im.paste(original_pic.im, (0, 0), mask.convert('L'))
- # # transparent_im.show()
- # # 上述抠图结果进行拼接
- # _img_im.paste(transparent_im, (0, 0), transparent_im)
- return _img_im
- def add_log(self, text, _type="info"):
- self.logger.info(
- f"旗舰抠图,{text}"
- )
- @func_set_timeout(40)
- def get_image_cut_ali(self, file_path):
- original_pic = Picture(in_path=file_path, im=None)
- original_pic.im = self.segment.get_image_orientation(original_pic.im)
- original_pic.x, original_pic.y = original_pic.im.size
- original_pic.im = original_pic.im.convert("RGB")
- image_deal_info = {}
- image_deal_info["原始图片大小"] = (original_pic.x, original_pic.y)
- # 原始图过小,则不需要使用阿里进行预处理
- if original_pic.x * original_pic.y < 1000000:
- cut_image = original_pic.im
- image_deal_info["抠图扩边后图片大小"] = cut_image.size
- image_deal_info["二次抠图是否缩放"] = False
- image_deal_info["抠图扩边后位置"] = (0, 0, original_pic.x, original_pic.y)
- else:
- self.add_log("开始预抠图处理")
- cut_image = self.r_ali.get_image_cut(
- file_path=None, out_file_path=None, original_im=original_pic.im
- )
- self.add_log("预抠图处理结束")
- x1, y1, x2, y2 = cut_image.getbbox()
- image_deal_info["鞋子原始位置"] = (x1, y1, x2, y2)
- o_w, o_h = cut_image.size
- image_deal_info["鞋子原始抠图后大小"] = (o_w, o_h)
- # 扩边处理
- _w, _h = x2 - x1, y2 - y1
- out_px = 0.025
- _w, _h = int(out_px * _w), int(out_px * _h)
- n_x1, n_y1, n_x2, n_y2 = x1 - _w, y1 - _h, x2 + _w, y2 + _h
- if n_x1 < 0:
- n_x1 = 0
- if n_y1 < 0:
- n_y1 = 0
- if n_x2 > o_w:
- n_x2 = o_w
- if n_y2 > o_h:
- n_y2 = o_h
- image_deal_info["抠图扩边后位置"] = (n_x1, n_y1, n_x2, n_y2)
- cut_image = original_pic.im.crop(image_deal_info["抠图扩边后位置"])
- image_deal_info["抠图扩边后图片大小"] = cut_image.size
- x, y = image_deal_info["抠图扩边后图片大小"]
- # 12000000
- max_size = settings.MAX_PIXIAN_SIZE
- if x * y > max_size:
- r = math.sqrt(max_size) / math.sqrt(x * y)
- r = r * 0.9
- size = (int(x * r), int(y * r))
- # print("图片:{} pixian触发二次缩放,原尺寸{}*{},新尺寸:{}".format(self.file_name, x, y, size))
- self.add_log(
- text="图片进行压缩,压缩前:{},压缩后:{}".format(
- image_deal_info["抠图扩边后图片大小"], size
- )
- )
- image_deal_info["抠图扩边后PIL对象"] = copy.deepcopy(cut_image)
- cut_image = cut_image.resize(size=size, resample=1)
- # print(cut_image.size)
- # print(image_deal_info["抠图扩边后PIL对象"].size)
- image_deal_info["二次抠图是否缩放"] = True
- else:
- image_deal_info["二次抠图是否缩放"] = False
- return cut_image, image_deal_info
- def picture_resize_to_original(self, _img, original_im):
- """
- Parameters
- ----------
- _img 需要还原的PIL对象
- original_im 原图对象
- Returns
- -------
- """
- # 将抠图结果转成mask
- # 将抠图结果放大到原始图大小
- _img = _img.resize(original_im.size, resample=1)
- new_big_mask = Image.new("RGB", _img.size, (0, 0, 0))
- white = Image.new("RGB", _img.size, (255, 255, 255))
- new_big_mask.paste(white, mask=_img.split()[3])
- # ---------制作选区缩小的mask
- mask = cv2.cvtColor(
- np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY
- ) # 将PIL 格式转换为 CV对象
- mask[mask != 255] = 0
- # 黑白反转
- # mask = 255 - mask
- # 选区缩小10
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
- erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
- # -------再进行抠图处理
- mask = Image.fromarray(
- cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)
- ) # CV 对象转 PIL
- transparent_im = Image.new("RGBA", original_im.size, (0, 0, 0, 0))
- transparent_im.paste(original_im, (0, 0), mask.convert("L"))
- # 上述抠图结果进行拼接
- _img.paste(transparent_im, (0, 0), transparent_im)
- return _img
- def run_ultra_segment(self, file_path, out_file_path):
- # 直接调用抠图
- time.sleep(0.01)
- # 1、增加获取key,2、key需要加密、3、429报错 重试再来拿一个KEY
- self.add_log("开始处理")
- cut_image_ali, image_deal_info = self.get_image_cut_ali(file_path)
- ultra_cutout_data = self.get_image_cut_ultra(out_file_path, cut_image_ali)
- if ultra_cutout_data is None:
- return None
- try:
- if image_deal_info["二次抠图是否缩放"]:
- # print("图片尺寸还原")
- self.add_log(text="图片尺寸进行还原")
- original_im = image_deal_info["抠图扩边后PIL对象"]
- second_cut_image = self.picture_resize_to_original(
- ultra_cutout_data, original_im
- )
- else:
- second_cut_image = ultra_cutout_data
- # 创建空白图片并粘贴回去
- _img_im = Image.new(
- mode="RGBA", size=image_deal_info["原始图片大小"], color=(0, 0, 0, 0)
- )
- _img_im.paste(
- second_cut_image,
- box=(
- image_deal_info["抠图扩边后位置"][0],
- image_deal_info["抠图扩边后位置"][1],
- ),
- )
- _img_im.save(out_file_path, dpi=(350, 350))
- return _img_im
- except BaseException as e:
- # print(e)
- text = "{} 图片处理错误,代码49990".format(e)
- self.add_log(text)
- return
- if __name__ == '__main__':
- r = RemoveUltraBackground()
- path = r"C:\Users\15001\Desktop\miniso\hb\原始图\hb(1).JPG"
- out_path = "{}._no_bg-out.png".format(path)
- r.get_image_cut(path, out_file_path=out_path)
|