Spaces:
Runtime error
Runtime error
Download visualize/visualize.py from Tharuneshwar/Text-Behind-Image: direct link, hf CLI and curl.
- Browser
- Download file 990 Bytes
-
https://huggingface.co/spaces/Tharuneshwar/Text-Behind-Image/resolve/main/visualize/visualize.py
- Command line
-
hf download hf://spaces/Tharuneshwar/Text-Behind-Image/visualize/visualize.py
-
curl -L -o visualize.py https://huggingface.co/spaces/Tharuneshwar/Text-Behind-Image/resolve/main/visualize/visualize.py
990 Bytes
| from PIL import Image | |
| import cv2 | |
| import numpy as np | |
| import torch | |
| from typing import Any, Dict, List | |
| def removeBgFromSegmentImage(og_image : Any, og_mask : Any, color: tuple = (0, 0, 255, 255), opacity: float = 0.2) -> Image: | |
| og_image = np.array(og_image.convert('RGB')) | |
| mask = og_mask.astype(np.uint8) * 255 # Convert to 0 or 255 | |
| rgba_image = np.zeros((og_image.shape[0], og_image.shape[1], 4), dtype=np.uint8) | |
| color_with_opacity = (color[0], color[1], color[2], int(color[3] * opacity)) | |
| rgba_image[mask > 0] = color_with_opacity | |
| return Image.fromarray(rgba_image) | |
| def removeOnlyBg(og_image : Any, og_mask : Any) -> Image: | |
| img = np.array(og_image.convert('RGB')) | |
| mask = cv2.resize(og_mask.astype(np.uint8), (img.shape[1], img.shape[0]), interpolation=cv2.INTER_NEAREST) | |
| rgba_image = np.zeros((img.shape[0], img.shape[1], 4), dtype=np.uint8) | |
| rgba_image[..., :3] = img | |
| rgba_image[..., 3] = mask * 255 | |
| return Image.fromarray(rgba_image) | |