-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIP_Grayscale.py
More file actions
76 lines (60 loc) · 2.23 KB
/
Copy pathPIP_Grayscale.py
File metadata and controls
76 lines (60 loc) · 2.23 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import torch
from PIL import Image
import numpy as np
class PIP_Grayscale:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "convert_to_grayscale"
CATEGORY = "图像处理"
def convert_to_grayscale(self, image):
# 确保图像是正确的维度 (batch_size, height, width, channels)
if image.dim() == 3:
image = image.unsqueeze(0)
# 获取批次大小
batch_size = image.shape[0]
# 创建一个空张量来存储结果
result = []
# 处理每个批次
for i in range(batch_size):
# 获取当前图像
img = image[i]
# 转换为PIL图像
pil_img = self._tensor_to_pil(img)
# 转换为灰度图像
# 使用 L 模式直接转换为灰度图,然后转回 RGB 以保持格式一致
gray_img = pil_img.convert('L').convert('RGB')
# 转回张量
gray_tensor = self._pil_to_tensor(gray_img)
# 添加到结果列表
result.append(gray_tensor)
# 将结果堆叠为批次
if len(result) > 0:
result_tensor = torch.stack(result)
else:
# 如果没有图像处理,返回空张量
result_tensor = torch.zeros((0, 0, 0, 3), dtype=torch.float32)
return (result_tensor,)
def _tensor_to_pil(self, tensor):
# 确保tensor在0-1范围内
tensor = tensor.clamp(0, 1)
# 转换为numpy数组并调整为0-255
img_np = (tensor.cpu().numpy() * 255).astype(np.uint8)
# 创建PIL图像
return Image.fromarray(img_np, mode='RGB')
def _pil_to_tensor(self, pil_img):
# 确保图像是RGB模式
if pil_img.mode != 'RGB':
pil_img = pil_img.convert('RGB')
# 转为numpy数组
img_np = np.array(pil_img).astype(np.float32) / 255.0
# 转为PyTorch张量
return torch.from_numpy(img_np)