-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBlackBorderCrop_node.py
More file actions
53 lines (42 loc) · 1.49 KB
/
BlackBorderCrop_node.py
File metadata and controls
53 lines (42 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
import numpy as np
from PIL import Image, ImageOps
class BlackBorderCrop:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"threshold": ("INT", {
"default": 10,
"min": 0,
"max": 255,
"step": 1,
"display": "slider"
}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "crop_black_border"
CATEGORY = "CGAnimittaTools"
def crop_black_border(self, image, threshold=10):
# Convert tensor to PIL image
image = image.squeeze().numpy()
image = (image * 255).astype(np.uint8)
pil_image = Image.fromarray(image)
# Convert to grayscale
grayscale = pil_image.convert("L")
# Create a binary mask of non-black regions
binary_mask = grayscale.point(lambda p: p > threshold and 255)
# Find the bounding box of non-black regions
bbox = binary_mask.getbbox()
if bbox:
# Crop the image to the bounding box
cropped_image = pil_image.crop(bbox)
else:
# If no bounding box found, return the original image
cropped_image = pil_image
# Convert back to tensor
cropped_image = np.array(cropped_image).astype(np.float32) / 255.0
cropped_image = torch.from_numpy(cropped_image).unsqueeze(0)
return (cropped_image,)