remove_bg_ali.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. import copy
  2. import json
  3. import os
  4. from PIL import Image
  5. from alibabacloud_imageseg20191230.client import Client as imageseg20191230Client
  6. from alibabacloud_imageseg20191230.models import SegmentCommodityAdvanceRequest
  7. from alibabacloud_imageseg20191230 import models as imageseg_20191230_models
  8. from alibabacloud_tea_util.models import RuntimeOptions
  9. from alibabacloud_tea_openapi import models as open_api_models
  10. from alibabacloud_tea_openapi.models import Config
  11. from alibabacloud_tea_util import models as util_models
  12. import requests
  13. from io import BytesIO
  14. import cv2
  15. import numpy as np
  16. from func_timeout import func_set_timeout
  17. from func_timeout import FunctionTimedOut
  18. from .multi_threaded_image_saving import ImageSaver
  19. import settings, time, math
  20. from logger import logger
  21. # 自己的
  22. AccessKeyId = "LTAI5tCk4p881X8hymj2FYFk"
  23. AccessKeySecret = "yBYIYzX8CL24r5ZgEx2AgZyDBmFkIK"
  24. def uploadImage(im: Image) -> str:
  25. img_byte_io = BytesIO()
  26. # 根据图片模式选择保存格式
  27. if im.mode == 'RGBA':
  28. im.save(img_byte_io, format='PNG')
  29. else:
  30. im.save(img_byte_io, format='JPEG')
  31. img_byte_io.seek(0) # 重置指针到开头
  32. post_headers = {"Authorization": settings.USER_TOKEN}
  33. url = settings.DOMAIN + "/api/upload"
  34. # 使用字节流上传
  35. resultData = requests.post(
  36. url,
  37. files={"file": ("image.jpg", img_byte_io, "image/jpeg")},
  38. headers=post_headers
  39. ).json()
  40. return resultData["data"]["url"]
  41. # 惠利玛公司的KEY
  42. # AccessKeyId = 'LTAI5tCk4p881X8hymj2FYFk'
  43. # AccessKeySecret = 'rQMgHwciTN4Gusbpt8CM8tflgsxh1V'
  44. # https://help.aliyun.com/zh/viapi/developer-reference/python?spm=a2c4g.11186623.0.i0#task-2252575
  45. # pip install alibabacloud_goodstech20191230
  46. # pip install alibabacloud_tea_openapi
  47. # pip install alibabacloud_tea_util
  48. class Segment(object):
  49. def __init__(self):
  50. self.client = self.create_client()
  51. def get_no_bg_common(self, file_path):
  52. # 初始化RuntimeObject
  53. runtime_option = RuntimeOptions()
  54. try:
  55. # 场景一:文件在本地
  56. img = open(file_path, 'rb')
  57. # 使用完成之后记得调用img.close()关闭流
  58. # 场景二,使用任意可访问的url
  59. # url = 'https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeBankCard/yhk1.jpg'
  60. # img = io.BytesIO(urlopen(url).read())
  61. # 4、初始化Request,这里只是以RecognizeBankCard为例,其他能力请使用相应能力对应的类
  62. request = SegmentCommodityAdvanceRequest()
  63. request.image_urlobject = img
  64. # 5、调用api,注意,recognize_bank_card_advance需要更换为相应能力对应的方法名。方法名是根据能力名称按照一定规范形成的,如能力名称为SegmentCommonImage,对应方法名应该为segment_common_image_advance。
  65. response = self.client.segment_common_image_advance(request, runtime_option)
  66. # 获取整体结果
  67. # print(response.body)
  68. img.close()
  69. return response.body
  70. # 获取单个字段,这里只是一个例子,具体能力下的字段需要看具体能力的文档
  71. # print(response.body.data.card_number)
  72. # tips: 可通过response.body.__dict__查看属性名称
  73. except Exception as error:
  74. # 获取整体报错信息
  75. print("error", error)
  76. return None
  77. # 获取单个字段
  78. # print(error.code)
  79. # tips: 可通过error.__dict__查看属性名称
  80. def get_image_orientation(self, img):
  81. # 获取EXIF数据
  82. exif = None
  83. try:
  84. if hasattr(img, 'getexif'):
  85. exif_data = img.getexif()
  86. if exif_data:
  87. exif = dict(exif_data)
  88. elif hasattr(img, '_getexif'):
  89. exif = img._getexif()
  90. except Exception:
  91. print("获取EXIF数据失败", img)
  92. if exif is not None:
  93. # EXIF标签274对应的是Orientation
  94. orientation = exif.get(0x0112)
  95. print("orientation", orientation)
  96. if orientation == 2:
  97. # 水平翻转
  98. img = img.transpose(Image.FLIP_LEFT_RIGHT)
  99. elif orientation == 3:
  100. # 旋转180度
  101. img = img.rotate(180, expand=True)
  102. elif orientation == 4:
  103. # 垂直翻转
  104. img = img.transpose(Image.FLIP_TOP_BOTTOM)
  105. elif orientation == 5:
  106. # 水平翻转后顺时针旋转90度
  107. img = img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
  108. elif orientation == 6:
  109. # 顺时针旋转90度
  110. img = img.transpose(Image.ROTATE_270)
  111. elif orientation == 7:
  112. # 水平翻转后逆时针旋转90度
  113. img = img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90)
  114. elif orientation == 8:
  115. # 逆时针旋转90度
  116. img = img.transpose(Image.ROTATE_90)
  117. else:
  118. print("没有EXIF数据或没有方向信息")
  119. orientation = 1
  120. return img
  121. def get_bo_bg_goods_ultra_background(self, im, is_shadow=False, api_url=None):
  122. imageUrl = uploadImage(im)
  123. # imageUrl = imageUrl + "?x-oss-process=image/auto-orient,1"
  124. if not settings.USER_TOKEN:
  125. print("错误:USER_TOKEN 未配置或为空")
  126. return None
  127. print("图片上传成功", imageUrl)
  128. post_headers = {"Authorization": "Bearer " + settings.USER_TOKEN}
  129. data = {
  130. "image_url": imageUrl,
  131. "is_shadow": is_shadow
  132. }
  133. response = requests.post(
  134. api_url, json=data, headers=post_headers
  135. )
  136. print(f"响应状态码: {response.status_code}")
  137. print(f"响应内容: {response.text[:500]}") # 只打印前500字符
  138. resultData = response.json()
  139. print("旗舰版抠图请求", resultData)
  140. # 安全地获取返回值
  141. data = resultData.get("data")
  142. if data is None:
  143. print("旗舰版抠图返回的data为空")
  144. return None
  145. cutout_image = data.get("cutout_image")
  146. if cutout_image is None:
  147. print("旗舰版抠图返回的cutout_image为空")
  148. return None
  149. return cutout_image
  150. def get_ultra_shadow(self, original_image, cutout_image, opacity, bright_target, api_url=None):
  151. image_original_url = uploadImage(original_image)
  152. image_cutcou_url = uploadImage(cutout_image)
  153. # imageUrl = imageUrl + "?x-oss-process=image/auto-orient,1"
  154. if not settings.USER_TOKEN:
  155. print("错误:USER_TOKEN 未配置或为空")
  156. return None
  157. print("阴影图处理,图片上传成功", image_original_url)
  158. post_headers = {"Authorization": "Bearer " + settings.USER_TOKEN}
  159. data = {
  160. "oraginal_image": image_original_url,
  161. "cutout_image": image_cutcou_url,
  162. "opacity": opacity,
  163. "bright_target": bright_target
  164. }
  165. print("阴影处理参数:", data)
  166. response = requests.post(
  167. api_url, json=data, headers=post_headers
  168. )
  169. print(f"响应状态码: {response.status_code}")
  170. print(f"响应内容: {response.text[:500]}") # 只打印前500字符
  171. resultData = response.json()
  172. print("旗舰版抠图请求", resultData)
  173. # 安全地获取返回值
  174. data = resultData.get("data")
  175. if data is None:
  176. print("旗舰版抠图返回的data为空")
  177. return None
  178. matte_image = data.get("matte_image")
  179. if matte_image is None:
  180. print("旗舰版 阴影处理 返回的 matte_image为空")
  181. return None
  182. return matte_image
  183. def get_no_bg_goods(self, file_path=None, _im=None):
  184. # https://blog.csdn.net/weixin_43411585/article/details/107780941
  185. im = _im
  186. # im.save(file_path)
  187. img = BytesIO()
  188. im.save(img, format='JPEG') # format: PNG or JPEG
  189. img.seek(0) # rewind to the start
  190. request = imageseg_20191230_models.SegmentCommodityAdvanceRequest()
  191. request.image_urlobject = img
  192. client = self.create_client()
  193. # 5、调用api,注意,recognize_bank_card_advance需要更换为相应能力对应的方法名。方法名是根据能力名称按照一定规范形成的,如能力名称为SegmentCommonImage,对应方法名应该为segment_common_image_advance。
  194. runtime = util_models.RuntimeOptions()
  195. response = client.segment_commodity_advance(request, runtime)
  196. # img.close()
  197. # print("1111111111111", response.body)
  198. return response.body
  199. def create_client(self):
  200. """
  201. 使用AK&SK初始化账号Client
  202. @param access_key_id:
  203. @param access_key_secret:
  204. @return: Client
  205. @throws Exception
  206. """
  207. config = open_api_models.Config(
  208. # 必填,您的 AccessKey ID,
  209. access_key_id=AccessKeyId,
  210. # 必填,您的 AccessKey Secret,
  211. access_key_secret=AccessKeySecret
  212. )
  213. # 访问的域名
  214. config.endpoint = f'imageseg.cn-shanghai.aliyuncs.com'
  215. return imageseg20191230Client(config)
  216. class Picture:
  217. def __init__(self, in_path, im=None):
  218. if im:
  219. self.im = im
  220. else:
  221. self.im = Image.open(in_path)
  222. self.x, self.y = self.im.size
  223. # print(self.x, self.y)
  224. def save_img(self, outpath, quality=90):
  225. # self.im = self.im.convert("RGB")
  226. self.im.save(outpath, quality=quality)
  227. def resize(self, width):
  228. re_x = int(width)
  229. re_y = int(self.y * re_x / self.x)
  230. self.im = self.im.resize((re_x, re_y), Image.BICUBIC)
  231. self.x, self.y = self.im.size
  232. def resize_by_heigh(self, heigh):
  233. re_y = int(heigh)
  234. re_x = int(self.x * re_y / self.y)
  235. self.im = self.im.resize((re_x, re_y), Image.BICUBIC)
  236. self.x, self.y = self.im.size
  237. class RemoveBgALi(object):
  238. def __init__(self):
  239. self.saver = ImageSaver()
  240. self.segment = Segment()
  241. @func_set_timeout(40)
  242. def get_image_cut_new(self, file_path, out_file_path=None, original_im=None):
  243. if original_im:
  244. original_pic = Picture(in_path=None, im=original_im)
  245. else:
  246. original_pic = Picture(file_path)
  247. if original_pic.im.mode != "RGB":
  248. original_pic.im = original_pic.im.convert("RGB")
  249. new_pic = copy.copy(original_pic)
  250. after_need_resize = False
  251. if new_pic.x > new_pic.y:
  252. if new_pic.x > 2000:
  253. after_need_resize = True
  254. new_pic.resize(2000)
  255. else:
  256. if new_pic.y > 2000:
  257. after_need_resize = True
  258. new_pic.resize_by_heigh(heigh=2000)
  259. # new_pic.im.show()
  260. body = self.segment.get_no_bg_goods(file_path=None, _im=new_pic.im)
  261. body = eval(str(body))
  262. try:
  263. image_url = body["Data"]["ImageURL"]
  264. except BaseException as e:
  265. print("阿里抠图错误:", e)
  266. # 处理失败,需要删除过程图片
  267. return None
  268. # 字节流转PIL对象
  269. response = requests.get(image_url)
  270. pic = response.content
  271. _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
  272. box_size = _img_im.getbbox()
  273. new_pp4_im = _img_im.crop(box_size)
  274. byte_io = BytesIO()
  275. new_pp4_im.save(byte_io, format='PNG') # 将图像保存为 PNG 格式到 BytesIO 对象
  276. byte_io.seek(0) # 将指针重置到流的开头,以便后续读取
  277. return byte_io
  278. @func_set_timeout(40)
  279. def get_image_cut(self, file_path, out_file_path=None, original_im=None):
  280. if original_im:
  281. original_pic = Picture(in_path=None, im=original_im)
  282. else:
  283. original_pic = Picture(file_path)
  284. if original_pic.im.mode != "RGB":
  285. print("抠图图片不能是PNG")
  286. return None
  287. new_pic = copy.copy(original_pic)
  288. after_need_resize = False
  289. if new_pic.x > new_pic.y:
  290. if new_pic.x > 2000:
  291. after_need_resize = True
  292. new_pic.resize(2000)
  293. else:
  294. if new_pic.y > 2000:
  295. after_need_resize = True
  296. new_pic.resize_by_heigh(heigh=2000)
  297. # new_pic.im.show()
  298. body = self.segment.get_no_bg_goods(file_path=None, _im=new_pic.im)
  299. body = eval(str(body))
  300. try:
  301. image_url = body["Data"]["ImageURL"]
  302. except BaseException as e:
  303. print("阿里抠图错误:", e)
  304. # 处理失败,需要删除过程图片
  305. return None
  306. # 字节流转PIL对象
  307. response = requests.get(image_url)
  308. pic = response.content
  309. _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
  310. # 原图更大,则需要执行CV处理
  311. if after_need_resize:
  312. # 将抠图结果转成mask
  313. # _img_im = Image.open(_path)
  314. # 将抠图结果放大到原始图大小
  315. _img_im = _img_im.resize(original_pic.im.size)
  316. new_big_mask = Image.new('RGB', _img_im.size, (0, 0, 0))
  317. white = Image.new('RGB', _img_im.size, (255, 255, 255))
  318. new_big_mask.paste(white, mask=_img_im.split()[3])
  319. # ---------制作选区缩小的mask
  320. # mask = cv2.imread(mask_path)
  321. # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
  322. mask = cv2.cvtColor(np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY) # 将PIL 格式转换为 CV对象
  323. mask[mask != 255] = 0
  324. # 黑白反转
  325. # mask = 255 - mask
  326. # 选区缩小10
  327. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
  328. erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
  329. # -------再进行抠图处理
  330. mask = Image.fromarray(cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)) # CV 对象转 PIL
  331. transparent_im = Image.new('RGBA', original_pic.im.size, (0, 0, 0, 0))
  332. # original_pic.im.show()
  333. # mask.show()
  334. transparent_im.paste(original_pic.im, (0, 0), mask.convert('L'))
  335. # transparent_im.show()
  336. # 上述抠图结果进行拼接
  337. _img_im.paste(transparent_im, (0, 0), transparent_im)
  338. # _img_im.show("11111111111111111111111")
  339. if out_file_path:
  340. self.saver.save_image(
  341. image=_img_im, file_path=out_file_path,
  342. quality=100, dpi=(350, 350), _format="PNG"
  343. )
  344. # _img_im.save(out_file_path)
  345. return _img_im
  346. def get_image_cut1(self, file_path, out_file_path=None):
  347. original_pic = Picture(file_path)
  348. new_pic = copy.copy(original_pic)
  349. if new_pic.x > 2000:
  350. new_pic.resize(2000)
  351. # new_pic.im.show()
  352. body = self.segment.get_no_bg_goods(file_path=out_file_path, _im=new_pic.im)
  353. body = eval(str(body))
  354. try:
  355. image_url = body["Data"]["ImageURL"]
  356. except BaseException as e:
  357. print("阿里抠图错误:", e)
  358. # 处理失败,需要删除过程图片
  359. return None
  360. # 字节流转PIL对象
  361. response = requests.get(image_url)
  362. pic = response.content
  363. _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
  364. if original_pic.x > 2000:
  365. # 原图更大,则需要执行CV处理
  366. # _img_im.show()
  367. # 对mask进行放大,然后进行抠图处理
  368. print("对mask进行放大,然后进行抠图处理")
  369. transparent_im = Image.new('RGBA', original_pic.im.size, (0, 0, 0, 0))
  370. # original_pic.im.show()
  371. # mask.show()
  372. _img_im = _img_im.resize((original_pic.x, original_pic.y))
  373. # _img_im.show()
  374. transparent_im.paste(original_pic.im, (0, 0), mask=_img_im)
  375. # transparent_im.show()
  376. # transparent_im.show()
  377. _img_im = transparent_im
  378. # 上述抠图结果进行拼接
  379. # _img_im.paste(transparent_im, (0, 0), transparent_im)
  380. pass
  381. _img_im.save(out_file_path)
  382. return _img_im
  383. def download_picture(self, url, out_path):
  384. response = requests.get(url)
  385. pic = response.content
  386. with open(out_path, 'wb') as f:
  387. f.write(pic)
  388. class RemoveUltraBackground:
  389. def __init__(self):
  390. self.api_url = '/api/ai_image/segment_shadow/segment_service'
  391. self.headers = {
  392. 'Content-Type': 'application/json',
  393. 'Accept': 'application/json'
  394. }
  395. self.saver = ImageSaver()
  396. self.segment = Segment()
  397. self.r_ali = RemoveBgALi()
  398. self.logger = logger
  399. @func_set_timeout(40)
  400. def get_image_cut_ultra(self, out_file_path=None, im_image=None):
  401. original_pic = Picture(in_path=None, im=im_image)
  402. original_pic.im = self.segment.get_image_orientation(im_image)
  403. original_pic.x, original_pic.y = original_pic.im.size
  404. if original_pic.im.mode != "RGB":
  405. print("抠图图片不能是PNG")
  406. return None
  407. new_pic = copy.copy(original_pic)
  408. # after_need_resize = False
  409. # if new_pic.x > new_pic.y:
  410. # if new_pic.x > 2000:
  411. # after_need_resize = True
  412. # new_pic.resize(2000)
  413. # else:
  414. # if new_pic.y > 2000:
  415. # after_need_resize = True
  416. # new_pic.resize_by_heigh(heigh=2000)
  417. print("使用旗舰版抠图")
  418. try:
  419. api_url = f"{settings.DOMAIN}{self.api_url}"
  420. image_url = self.segment.get_bo_bg_goods_ultra_background(im=new_pic.im, api_url=api_url)
  421. except BaseException as e:
  422. print("旗舰版抠图异常:", e)
  423. # 处理失败,需要删除过程图片
  424. return None
  425. if image_url is None:
  426. return None
  427. # 字节流转PIL对象
  428. print("image_url", image_url)
  429. response = requests.get(image_url)
  430. pic = response.content
  431. _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
  432. # # 原图更大,则需要执行CV处理
  433. # if after_need_resize:
  434. # # 将抠图结果转成mask
  435. # # _img_im = Image.open(_path)
  436. # # 将抠图结果放大到原始图大小
  437. # _img_im = _img_im.resize(original_pic.im.size)
  438. # new_big_mask = Image.new('RGB', _img_im.size, (0, 0, 0))
  439. # white = Image.new('RGB', _img_im.size, (255, 255, 255))
  440. # new_big_mask.paste(white, mask=_img_im.split()[3])
  441. #
  442. # # ---------制作选区缩小的mask
  443. # # mask = cv2.imread(mask_path)
  444. # # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
  445. # mask = cv2.cvtColor(np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY) # 将PIL 格式转换为 CV对象
  446. # mask[mask != 255] = 0
  447. # # 黑白反转
  448. # # mask = 255 - mask
  449. # # 选区缩小10
  450. # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
  451. # erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
  452. #
  453. # # -------再进行抠图处理
  454. # mask = Image.fromarray(cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)) # CV 对象转 PIL
  455. # transparent_im = Image.new('RGBA', original_pic.im.size, (0, 0, 0, 0))
  456. # # original_pic.im.show()
  457. # # mask.show()
  458. # transparent_im.paste(original_pic.im, (0, 0), mask.convert('L'))
  459. # # transparent_im.show()
  460. # # 上述抠图结果进行拼接
  461. # _img_im.paste(transparent_im, (0, 0), transparent_im)
  462. return _img_im
  463. def add_log(self, text, _type="info"):
  464. self.logger.info(
  465. f"旗舰抠图,{text}"
  466. )
  467. @func_set_timeout(40)
  468. def get_image_cut_ali(self, file_path):
  469. original_pic = Picture(in_path=file_path, im=None)
  470. original_pic.im = self.segment.get_image_orientation(original_pic.im)
  471. original_pic.x, original_pic.y = original_pic.im.size
  472. original_pic.im = original_pic.im.convert("RGB")
  473. image_deal_info = {}
  474. image_deal_info["原始图片大小"] = (original_pic.x, original_pic.y)
  475. # 原始图过小,则不需要使用阿里进行预处理
  476. if original_pic.x * original_pic.y < 1000000:
  477. cut_image = original_pic.im
  478. image_deal_info["抠图扩边后图片大小"] = cut_image.size
  479. image_deal_info["二次抠图是否缩放"] = False
  480. image_deal_info["抠图扩边后位置"] = (0, 0, original_pic.x, original_pic.y)
  481. else:
  482. self.add_log("开始预抠图处理")
  483. cut_image = self.r_ali.get_image_cut(
  484. file_path=None, out_file_path=None, original_im=original_pic.im
  485. )
  486. self.add_log("预抠图处理结束")
  487. x1, y1, x2, y2 = cut_image.getbbox()
  488. image_deal_info["鞋子原始位置"] = (x1, y1, x2, y2)
  489. o_w, o_h = cut_image.size
  490. image_deal_info["鞋子原始抠图后大小"] = (o_w, o_h)
  491. # 扩边处理
  492. _w, _h = x2 - x1, y2 - y1
  493. out_px = 0.025
  494. _w, _h = int(out_px * _w), int(out_px * _h)
  495. n_x1, n_y1, n_x2, n_y2 = x1 - _w, y1 - _h, x2 + _w, y2 + _h
  496. if n_x1 < 0:
  497. n_x1 = 0
  498. if n_y1 < 0:
  499. n_y1 = 0
  500. if n_x2 > o_w:
  501. n_x2 = o_w
  502. if n_y2 > o_h:
  503. n_y2 = o_h
  504. image_deal_info["抠图扩边后位置"] = (n_x1, n_y1, n_x2, n_y2)
  505. cut_image = original_pic.im.crop(image_deal_info["抠图扩边后位置"])
  506. image_deal_info["抠图扩边后图片大小"] = cut_image.size
  507. x, y = image_deal_info["抠图扩边后图片大小"]
  508. # 12000000
  509. max_size = settings.MAX_PIXIAN_SIZE
  510. if x * y > max_size:
  511. r = math.sqrt(max_size) / math.sqrt(x * y)
  512. r = r * 0.9
  513. size = (int(x * r), int(y * r))
  514. # print("图片:{} pixian触发二次缩放,原尺寸{}*{},新尺寸:{}".format(self.file_name, x, y, size))
  515. self.add_log(
  516. text="图片进行压缩,压缩前:{},压缩后:{}".format(
  517. image_deal_info["抠图扩边后图片大小"], size
  518. )
  519. )
  520. image_deal_info["抠图扩边后PIL对象"] = copy.deepcopy(cut_image)
  521. cut_image = cut_image.resize(size=size, resample=1)
  522. # print(cut_image.size)
  523. # print(image_deal_info["抠图扩边后PIL对象"].size)
  524. image_deal_info["二次抠图是否缩放"] = True
  525. else:
  526. image_deal_info["二次抠图是否缩放"] = False
  527. return cut_image, image_deal_info
  528. def picture_resize_to_original(self, _img, original_im):
  529. """
  530. Parameters
  531. ----------
  532. _img 需要还原的PIL对象
  533. original_im 原图对象
  534. Returns
  535. -------
  536. """
  537. # 将抠图结果转成mask
  538. # 将抠图结果放大到原始图大小
  539. _img = _img.resize(original_im.size, resample=1)
  540. new_big_mask = Image.new("RGB", _img.size, (0, 0, 0))
  541. white = Image.new("RGB", _img.size, (255, 255, 255))
  542. new_big_mask.paste(white, mask=_img.split()[3])
  543. # ---------制作选区缩小的mask
  544. mask = cv2.cvtColor(
  545. np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY
  546. ) # 将PIL 格式转换为 CV对象
  547. mask[mask != 255] = 0
  548. # 黑白反转
  549. # mask = 255 - mask
  550. # 选区缩小10
  551. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
  552. erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
  553. # -------再进行抠图处理
  554. mask = Image.fromarray(
  555. cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)
  556. ) # CV 对象转 PIL
  557. transparent_im = Image.new("RGBA", original_im.size, (0, 0, 0, 0))
  558. transparent_im.paste(original_im, (0, 0), mask.convert("L"))
  559. # 上述抠图结果进行拼接
  560. _img.paste(transparent_im, (0, 0), transparent_im)
  561. return _img
  562. @func_set_timeout(40)
  563. def ultra_segment_fast(self, file_path, out_file_path=None):
  564. '''
  565. 旗舰抠图快速版
  566. '''
  567. original_pic = Picture(file_path)
  568. original_pic.im = self.segment.get_image_orientation(original_pic.im)
  569. original_pic.x, original_pic.y = original_pic.im.size
  570. if original_pic.im.mode != "RGB":
  571. print("抠图图片不能是PNG")
  572. return None
  573. new_pic = copy.copy(original_pic)
  574. after_need_resize = False
  575. if new_pic.x > new_pic.y:
  576. if new_pic.x > 2000:
  577. after_need_resize = True
  578. new_pic.resize(2000)
  579. else:
  580. if new_pic.y > 2000:
  581. after_need_resize = True
  582. new_pic.resize_by_heigh(heigh=2000)
  583. print("使用旗舰版抠图")
  584. try:
  585. api_url = f"{settings.DOMAIN}{self.api_url}"
  586. image_url = self.segment.get_bo_bg_goods_ultra_background(im=new_pic.im, api_url=api_url)
  587. except BaseException as e:
  588. print("旗舰版抠图异常:", e)
  589. # 处理失败,需要删除过程图片
  590. return None
  591. if image_url is None:
  592. return None
  593. # 字节流转PIL对象
  594. print("image_url", image_url)
  595. response = requests.get(image_url)
  596. pic = response.content
  597. _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
  598. # 原图更大,则需要执行CV处理
  599. if after_need_resize:
  600. # 将抠图结果转成mask
  601. # _img_im = Image.open(_path)
  602. # 将抠图结果放大到原始图大小
  603. _img_im = _img_im.resize(original_pic.im.size)
  604. new_big_mask = Image.new('RGB', _img_im.size, (0, 0, 0))
  605. white = Image.new('RGB', _img_im.size, (255, 255, 255))
  606. new_big_mask.paste(white, mask=_img_im.split()[3])
  607. # ---------制作选区缩小的mask
  608. # mask = cv2.imread(mask_path)
  609. # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
  610. mask = cv2.cvtColor(np.asarray(new_big_mask), cv2.COLOR_BGR2GRAY) # 将PIL 格式转换为 CV对象
  611. mask[mask != 255] = 0
  612. # 黑白反转
  613. # mask = 255 - mask
  614. # 选区缩小10
  615. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
  616. erode_im = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
  617. # -------再进行抠图处理
  618. mask = Image.fromarray(cv2.cvtColor(erode_im, cv2.COLOR_GRAY2RGBA)) # CV 对象转 PIL
  619. transparent_im = Image.new('RGBA', original_pic.im.size, (0, 0, 0, 0))
  620. # original_pic.im.show()
  621. # mask.show()
  622. transparent_im.paste(original_pic.im, (0, 0), mask.convert('L'))
  623. # transparent_im.show()
  624. # 上述抠图结果进行拼接
  625. _img_im.paste(transparent_im, (0, 0), transparent_im)
  626. # 原图更大,则需要执行CV处理
  627. if out_file_path:
  628. self.saver.save_image(
  629. image=_img_im, file_path=out_file_path,
  630. quality=100, dpi=(350, 350), _format="PNG"
  631. )
  632. return _img_im
  633. def run_ultra_segment(self, file_path, out_file_path):
  634. # 直接调用抠图
  635. time.sleep(0.01)
  636. # 1、增加获取key,2、key需要加密、3、429报错 重试再来拿一个KEY
  637. self.add_log("开始处理")
  638. cut_image_ali, image_deal_info = self.get_image_cut_ali(file_path)
  639. ultra_cutout_data = self.get_image_cut_ultra(out_file_path, cut_image_ali)
  640. if ultra_cutout_data is None:
  641. return None
  642. try:
  643. if image_deal_info["二次抠图是否缩放"]:
  644. # print("图片尺寸还原")
  645. self.add_log(text="图片尺寸进行还原")
  646. original_im = image_deal_info["抠图扩边后PIL对象"]
  647. second_cut_image = self.picture_resize_to_original(
  648. ultra_cutout_data, original_im
  649. )
  650. else:
  651. second_cut_image = ultra_cutout_data
  652. # 创建空白图片并粘贴回去
  653. _img_im = Image.new(
  654. mode="RGBA", size=image_deal_info["原始图片大小"], color=(0, 0, 0, 0)
  655. )
  656. _img_im.paste(
  657. second_cut_image,
  658. box=(
  659. image_deal_info["抠图扩边后位置"][0],
  660. image_deal_info["抠图扩边后位置"][1],
  661. ),
  662. )
  663. _img_im.save(out_file_path, dpi=(350, 350))
  664. return _img_im
  665. except BaseException as e:
  666. # print(e)
  667. text = "{} 图片处理错误,代码49990".format(e)
  668. self.add_log(text)
  669. return
  670. if __name__ == '__main__':
  671. r = RemoveUltraBackground()
  672. path = r"C:\Users\15001\Desktop\miniso\hb\原始图\hb(1).JPG"
  673. out_path = "{}._no_bg-out.png".format(path)
  674. r.get_image_cut(path, out_file_path=out_path)