remove_bg_pixian.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import copy
  2. import os
  3. from PIL import Image
  4. from .remove_bg_ali import RemoveBgALi
  5. import requests
  6. from io import BytesIO
  7. import settings
  8. class Segment(object):
  9. def __init__(self):
  10. self.k = "pxnib99dbchtmdm"
  11. self.s = "ub9uj5678gs4m2bnrass1t3tn6ughlk065ianosk06akagolcr2u"
  12. def get_no_bg_goods2(self, file_path=None, _im=None):
  13. im = _im
  14. img = BytesIO()
  15. try:
  16. im.save(img, format='JPEG') # format: PNG or JPEG
  17. except:
  18. im.save(img, format='PNG') # format: PNG or JPEG
  19. img.seek(0) # rewind to the start
  20. # img = "https://ossimg.valimart.net/uploads/vali_ai/20241011/172864207036098.png"
  21. response = requests.post(
  22. 'http://47.76.110.118:27777/api/v2/remove-background',
  23. files={'image.url': img},
  24. data={
  25. # Add more upload options here
  26. },
  27. auth=(self.k, self.s)
  28. )
  29. # print(response.content)
  30. if response.status_code == requests.codes.ok:
  31. return response.content, ""
  32. else:
  33. return None, response.content
  34. def get_no_bg_goods_by_url(self, url,key):
  35. if key:
  36. # 切换key
  37. auth = key
  38. # auth = (self.k, self.s)
  39. else:
  40. auth = (self.k, self.s)
  41. response = requests.post(
  42. 'https://3api.valimart.net/api/v2/remove-background',
  43. data={
  44. 'image.url': url
  45. },
  46. auth=auth
  47. )
  48. data = {"im": None,
  49. "status_code": response.status_code, }
  50. try:
  51. if response.status_code == requests.codes.ok:
  52. data["im"] = Image.open(BytesIO(response.content))
  53. return data
  54. else:
  55. print("response.status_code:", response.status_code)
  56. data = {"im": None,
  57. "status_code": "time_out",
  58. "message":"处理失败"
  59. }
  60. return data
  61. except BaseException as e:
  62. data = {"im": None,
  63. "status_code": "time_out",
  64. "message":"{}".format(e)
  65. }
  66. return data
  67. def get_no_bg_goods(self, file_path=None, _im=None, key=None):
  68. im = _im
  69. img = BytesIO()
  70. try:
  71. im.save(img, format='JPEG') # format: PNG or JPEG
  72. except:
  73. im.save(img, format='PNG') # format: PNG or JPEG
  74. img.seek(0) # rewind to the start
  75. if key:
  76. # 切换key
  77. auth = key
  78. # auth = (self.k, self.s)
  79. else:
  80. auth = (self.k, self.s)
  81. try:
  82. response = requests.post(
  83. 'https://3api.valimart.net/api/v2/remove-background',
  84. files={'image': img},
  85. data={
  86. # Add more upload options here
  87. },
  88. auth=auth,
  89. timeout=40
  90. )
  91. except BaseException as e:
  92. data = {"im": None,
  93. "status_code": "time_out",
  94. "message":"{}".format(e)
  95. }
  96. return data
  97. # print(response.content)
  98. data = {"im": None,
  99. "status_code": response.status_code, }
  100. if response.status_code == requests.codes.ok:
  101. data["im"] = Image.open(BytesIO(response.content))
  102. return data
  103. class Picture:
  104. def __init__(self, in_path, im=None):
  105. if im:
  106. self.im = im
  107. else:
  108. self.im = Image.open(in_path)
  109. self.x, self.y = self.im.size
  110. # print(self.x, self.y)
  111. def save_img(self, outpath, quality=90):
  112. # self.im = self.im.convert("RGB")
  113. self.im.save(outpath, quality=quality)
  114. def resize(self, width):
  115. re_x = int(width)
  116. re_y = int(self.y * re_x / self.x)
  117. self.im = self.im.resize((re_x, re_y), Image.BICUBIC)
  118. self.x, self.y = self.im.size
  119. def resize_by_heigh(self, heigh):
  120. re_y = int(heigh)
  121. re_x = int(self.x * re_y / self.y)
  122. self.im = self.im.resize((re_x, re_y), Image.BICUBIC)
  123. self.x, self.y = self.im.size
  124. class RemoveBgPiXian(object):
  125. def __init__(self):
  126. self.segment = Segment()
  127. self.r = RemoveBgALi()
  128. def direct_matting_image(self, image):
  129. #不能超过32,000,000尺寸的数据
  130. x, y = image.size
  131. f = False
  132. if x * y > 32000000:
  133. r = 32000000 / x * y
  134. image = image.resize(size=(int(x * r), int(y * r)))
  135. f = True
  136. pic, _ = self.segment.get_no_bg_goods(file_path=None, _im=image)
  137. if not pic:
  138. return None, _
  139. _img_im = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
  140. if f:
  141. _img_im = _img_im.resize(size=(x, y))
  142. return _img_im, ""
  143. def run_by_image_url(self, url):
  144. pic, _ = self.segment.get_no_bg_goods_by_url(url)
  145. if pic is not None:
  146. _img_im = Image.open(BytesIO(pic))
  147. return _img_im, None
  148. else:
  149. return None, _
  150. def upload_image_by_io(self, image_pil:Image) -> str:
  151. # post_headers = {"Authorization": settings.Authorization}
  152. im = image_pil
  153. img = BytesIO()
  154. try:
  155. im.save(img, format='JPEG') # format: PNG or JPEG
  156. except:
  157. im.save(img, format='PNG') # format: PNG or JPEG
  158. img.seek(0) # rewind to the start
  159. try:
  160. url = settings.DOMAIN + "/api/upload"
  161. resultData = requests.post(
  162. url, files={"file": img},
  163. timeout=100
  164. ).json()
  165. return resultData["data"]["url"]
  166. except Exception as e:
  167. print("upload_image_by_io error:", e)
  168. return None
  169. def run_by_image_im(self, im, key):
  170. image_url = self.upload_image_by_io(im)
  171. if image_url is None:
  172. data = {"im": None,
  173. "status_code": "time_out",
  174. "message":"图片上传失败"
  175. }
  176. return data
  177. # image_url
  178. # 把image_url中的ossimg.valimart.net
  179. # 替换为img-cuts.valimart.net
  180. print("image_url 1:", image_url)
  181. image_url = image_url.replace('ossimg.valimart.net', 'img-cuts.valimart.net')
  182. print("image_url 2:", image_url)
  183. return self.segment.get_no_bg_goods_by_url(url=image_url,key=key)
  184. def get_image_cut(self, file_path, out_file_path=None, original_im=None, image_preprocessing=False, is_test=False):
  185. if original_im:
  186. original_pic = Picture(in_path=None, im=original_im)
  187. else:
  188. original_pic = Picture(file_path)
  189. if original_pic.im.mode != "RGB":
  190. print("抠图图片不能是PNG")
  191. return False, {"data": "抠图图片不能是PNG"}
  192. if is_test:
  193. cut_image = self.r.get_image_cut(file_path=None, out_file_path=None, original_im=original_pic.im)
  194. if out_file_path:
  195. cut_image.save(out_file_path)
  196. return True, {}
  197. if image_preprocessing:
  198. cut_image = self.r.get_image_cut(file_path=None, out_file_path=None, original_im=original_pic.im)
  199. image_deal_info = {}
  200. x1, y1, x2, y2 = cut_image.getbbox()
  201. image_deal_info["鞋子原始位置"] = (x1, y1, x2, y2)
  202. o_w, o_h = cut_image.size
  203. image_deal_info["鞋子原始抠图后大小"] = (o_w, o_h)
  204. # 扩边处理
  205. _w, _h = x2 - x1, y2 - y1
  206. out_px = 0.06
  207. _w, _h = int(out_px * _w), int(out_px * _h)
  208. n_x1, n_y1, n_x2, n_y2 = x1 - _w, y1 - _h, x2 + _w, y2 + _h
  209. if n_x1 < 0:
  210. n_x1 = 0
  211. if n_y1 < 0:
  212. n_y1 = 0
  213. if n_x2 > o_w:
  214. n_x2 = o_w
  215. if n_y2 > o_h:
  216. n_y2 = o_h
  217. image_deal_info["抠图扩边后位置"] = (n_x1, n_y1, n_x2, n_y2)
  218. cut_image = original_pic.im.crop(image_deal_info["抠图扩边后位置"])
  219. image_deal_info["抠图扩边后图片大小"] = cut_image.size
  220. image_deal_info["原始图片大小"] = (original_pic.x, original_pic.y)
  221. # 使用pixian进行抠图
  222. second_cut_image, _ = self.direct_matting_image(image=cut_image)
  223. if not second_cut_image:
  224. return False, {"data": _}
  225. if second_cut_image.size != image_deal_info["抠图扩边后图片大小"]:
  226. print("图片尺寸还原")
  227. second_cut_image = second_cut_image.resize(image_deal_info["抠图扩边后图片大小"])
  228. # 创建空白图片并粘贴回去
  229. _img_im = Image.new(mode="RGBA", size=image_deal_info["原始图片大小"], color=(0, 0, 0, 0))
  230. _img_im.paste(second_cut_image, box=(image_deal_info["抠图扩边后位置"][0], image_deal_info["抠图扩边后位置"][1]))
  231. else:
  232. _img_im = self.direct_matting_image(image=original_pic.im)
  233. pass
  234. if out_file_path:
  235. _img_im.save(out_file_path)
  236. return True, {}
  237. def download_picture(self, url, out_path):
  238. response = requests.get(url)
  239. pic = response.content
  240. with open(out_path, 'wb') as f:
  241. f.write(pic)
  242. if __name__ == '__main__':
  243. r = RemoveBgPiXian()
  244. path = r"C:\Users\gymmc\Desktop\白底部分品类45度\测试\eva_keai_1.png"
  245. out_path = "{}._no_bg-out.png".format(path)
  246. r.get_image_cut(path, out_file_path=out_path)