增加节点解码说明

This commit is contained in:
engineer6980
2026-02-04 11:46:39 +08:00
parent 2f60b4aa0f
commit ffcaad07ac
+26 -21
View File
@@ -1,7 +1,7 @@
import torch
# ==========================================
# 🔑 全局密钥:必须保持一致
# 🔑 固定密钥
# ==========================================
SECRET_KEY = 999
@@ -12,9 +12,28 @@ class ImageCipherEncode:
@classmethod
def INPUT_TYPES(s):
# 这里定义显示在节点上的文字内容
info_text = (
"📌【前端解码指南 / Frontend Guide】\n"
"本节点专为API工作流设计,前端需按以下算法还原:\n"
"------------------------------------------------\n"
"1. 固定密钥 (Key): 999\n"
"2. 算法原理: RGB通道独立行位移 (Row Rolling)\n"
"3. 核心公式 (y=行号, w=图像宽度):\n"
" R_shift = (y * 999 * 1) % w\n"
" G_shift = (y * 999 * 2) % w\n"
" B_shift = (y * 999 * 3) % w\n"
"4. 操作: 将对应通道的每行像素向左(反向)回滚 shift 距离\n"
"------------------------------------------------"
)
return {
"required": {
"images": ("IMAGE",),
},
# 添加这个可选输入,专门用于显示文字
"optional": {
"decryption_guide": ("STRING", {"default": info_text, "multiline": True}),
}
}
@@ -23,25 +42,20 @@ class ImageCipherEncode:
FUNCTION = "encode"
CATEGORY = "CipherTools"
def encode(self, images):
# 注意:函数签名里要加上 decryption_guide,尽管我们在代码里不使用它
def encode(self, images, decryption_guide=""):
batch, h, w, c = images.shape
out_list = []
base_shift = SECRET_KEY
for i in range(batch):
img = images[i]
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
# 分离通道
r = img[:, :, 0]
g = img[:, :, 1]
b = img[:, :, 2]
# 准备容器
r_enc = torch.zeros_like(r)
g_enc = torch.zeros_like(g)
b_enc = torch.zeros_like(b)
# 加密循环:正向位移 (shifts=正数)
for y in range(h):
shift_r = (y * base_shift * 1) % w
shift_g = (y * base_shift * 2) % w
@@ -56,17 +70,14 @@ class ImageCipherEncode:
return (torch.stack(out_list, dim=0),)
# 解码节点保持不变,这里省略以节省篇幅,请保留你原来的 ImageCipherDecode 类
class ImageCipherDecode:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE",),
}
}
return {"required": {"images": ("IMAGE",), }}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("restored_image",)
@@ -80,26 +91,20 @@ class ImageCipherDecode:
for i in range(batch):
img = images[i]
r_enc = img[:, :, 0]
g_enc = img[:, :, 1]
b_enc = img[:, :, 2]
r_enc, g_enc, b_enc = img[:, :, 0], img[:, :, 1], img[:, :, 2]
r_dec = torch.zeros_like(r_enc)
g_dec = torch.zeros_like(g_enc)
b_dec = torch.zeros_like(b_enc)
# 解密循环:反向位移 (shifts=负数)
for y in range(h):
shift_r = (y * base_shift * 1) % w
shift_g = (y * base_shift * 2) % w
shift_b = (y * base_shift * 3) % w
# 注意这里的负号 -shift
r_dec[y, :] = torch.roll(r_enc[y, :], shifts=-shift_r, dims=0)
g_dec[y, :] = torch.roll(g_enc[y, :], shifts=-shift_g, dims=0)
b_dec[y, :] = torch.roll(b_enc[y, :], shifts=-shift_b, dims=0)
out_list.append(torch.stack((r_dec, g_dec, b_dec), dim=-1))
return (torch.stack(out_list, dim=0),)