Skip to content

Commit

Permalink
persisting background image fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Taehun Kim authored and Taehun Kim committed Oct 20, 2023
1 parent 24fc382 commit 94f9441
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name="transparent-background",
version="1.2.8",
version="1.2.9",
author="Taehun Kim",
author_email="taehoon1018@postech.ac.kr",
description="Make images with transparent background",
Expand Down
19 changes: 14 additions & 5 deletions transparent_background/Remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(self, mode="base", jit=False, device=None, ckpt=None, fast=None):
]
)

self.background = None
self.background = {'img': None, 'name': None, 'shape': None}
desc = "Mode={}, Device={}, Torchscript={}".format(
mode, self.device, "enabled" if jit else "disabled"
)
Expand Down Expand Up @@ -198,10 +198,19 @@ def process(self, img, type="rgba", threshold=None):
img[border != 0] = [120, 255, 155]

elif type.lower().endswith((".jpg", ".jpeg", ".png")):
if self.background is None:
self.background = cv2.cvtColor(cv2.imread(type), cv2.COLOR_BGR2RGB)
self.background = cv2.resize(self.background, img.shape[:2][::-1])
img = img * pred[..., np.newaxis] + self.background * (
if self.background['name'] != type:
background_img = cv2.cvtColor(cv2.imread(type), cv2.COLOR_BGR2RGB)
background_img = cv2.resize(background_img, img.shape[:2][::-1])

self.background['img'] = background_img
self.background['shape'] = img.shape[:2][::-1]
self.background['name'] = type

elif self.background['shape'] != img.shape[:2][::-1]:
self.background['img'] = cv2.resize(self.background['img'], img.shape[:2][::-1])
self.background['shape'] = img.shape[:2][::-1]

img = img * pred[..., np.newaxis] + self.background['img'] * (
1 - pred[..., np.newaxis]
)

Expand Down

0 comments on commit 94f9441

Please sign in to comment.