Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
@@ -64,6 +64,8 @@ class WatermarkProcessor:
|
|
64 |
except Exception as e:
|
65 |
return im_name, f"Error adding watermark: {str(e)}"
|
66 |
|
|
|
|
|
67 |
def encode(self, image_path, watermark_text, metadata=None):
|
68 |
"""Encode watermark using simple LSB steganography"""
|
69 |
try:
|
@@ -78,14 +80,15 @@ class WatermarkProcessor:
|
|
78 |
'metadata': metadata or {}
|
79 |
}
|
80 |
|
81 |
-
# Convert to string
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
86 |
|
87 |
# Check capacity
|
88 |
-
if len(
|
89 |
return image_path, "Error: Image too small for watermark data"
|
90 |
|
91 |
# Embed data
|
@@ -93,17 +96,15 @@ class WatermarkProcessor:
|
|
93 |
for i in range(image.shape[0]):
|
94 |
for j in range(image.shape[1]):
|
95 |
for k in range(3):
|
96 |
-
if data_index < len(
|
97 |
-
#
|
98 |
-
|
99 |
-
pixel = (pixel & 0xFE) | int(binary_secret[data_index])
|
100 |
-
image[i, j, k] = pixel
|
101 |
data_index += 1
|
102 |
else:
|
103 |
break
|
104 |
-
if data_index >= len(
|
105 |
break
|
106 |
-
if data_index >= len(
|
107 |
break
|
108 |
|
109 |
# Save result
|
@@ -117,7 +118,7 @@ class WatermarkProcessor:
|
|
117 |
def decode(self, image_path):
|
118 |
"""Decode watermark using simple LSB steganography"""
|
119 |
try:
|
120 |
-
# Try PNG metadata
|
121 |
try:
|
122 |
im = Image.open(image_path)
|
123 |
if "TXT" in im.info:
|
@@ -125,44 +126,42 @@ class WatermarkProcessor:
|
|
125 |
except:
|
126 |
pass
|
127 |
|
128 |
-
# Read image
|
129 |
image = cv2.imread(image_path)
|
130 |
if image is None:
|
131 |
raise ValueError("Could not read image file")
|
132 |
|
133 |
-
# Extract
|
134 |
-
|
135 |
for i in range(image.shape[0]):
|
136 |
for j in range(image.shape[1]):
|
137 |
for k in range(3):
|
138 |
-
|
139 |
|
140 |
# Convert binary to text
|
141 |
-
|
142 |
-
for i in range(0, len(
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
except json.JSONDecodeError:
|
159 |
-
return text
|
160 |
|
161 |
return "Error: No valid watermark found"
|
162 |
|
163 |
except Exception as e:
|
164 |
return f"Error in decoding: {str(e)}"
|
165 |
|
|
|
166 |
def analyze_quality(self, original_path, watermarked_path):
|
167 |
"""Analyze watermark quality"""
|
168 |
try:
|
|
|
64 |
except Exception as e:
|
65 |
return im_name, f"Error adding watermark: {str(e)}"
|
66 |
|
67 |
+
# utils.py의 encode와 decode 메서드만 수정
|
68 |
+
|
69 |
def encode(self, image_path, watermark_text, metadata=None):
|
70 |
"""Encode watermark using simple LSB steganography"""
|
71 |
try:
|
|
|
80 |
'metadata': metadata or {}
|
81 |
}
|
82 |
|
83 |
+
# Convert data to string with end marker
|
84 |
+
json_str = json.dumps(data, ensure_ascii=False)
|
85 |
+
secret_data = json_str + "#####"
|
86 |
+
|
87 |
+
# Convert string to binary (using utf-8 encoding)
|
88 |
+
binary_data = ''.join(format(ord(char), '08b') for char in secret_data)
|
89 |
|
90 |
# Check capacity
|
91 |
+
if len(binary_data) > image.shape[0] * image.shape[1] * 3:
|
92 |
return image_path, "Error: Image too small for watermark data"
|
93 |
|
94 |
# Embed data
|
|
|
96 |
for i in range(image.shape[0]):
|
97 |
for j in range(image.shape[1]):
|
98 |
for k in range(3):
|
99 |
+
if data_index < len(binary_data):
|
100 |
+
# Replace LSB with watermark bit
|
101 |
+
image[i, j, k] = np.uint8((image[i, j, k] & ~1) | int(binary_data[data_index]))
|
|
|
|
|
102 |
data_index += 1
|
103 |
else:
|
104 |
break
|
105 |
+
if data_index >= len(binary_data):
|
106 |
break
|
107 |
+
if data_index >= len(binary_data):
|
108 |
break
|
109 |
|
110 |
# Save result
|
|
|
118 |
def decode(self, image_path):
|
119 |
"""Decode watermark using simple LSB steganography"""
|
120 |
try:
|
121 |
+
# Try PNG metadata first
|
122 |
try:
|
123 |
im = Image.open(image_path)
|
124 |
if "TXT" in im.info:
|
|
|
126 |
except:
|
127 |
pass
|
128 |
|
|
|
129 |
image = cv2.imread(image_path)
|
130 |
if image is None:
|
131 |
raise ValueError("Could not read image file")
|
132 |
|
133 |
+
# Extract binary data
|
134 |
+
binary_data = ''
|
135 |
for i in range(image.shape[0]):
|
136 |
for j in range(image.shape[1]):
|
137 |
for k in range(3):
|
138 |
+
binary_data += str(image[i, j, k] & 1)
|
139 |
|
140 |
# Convert binary to text
|
141 |
+
text = ''
|
142 |
+
for i in range(0, len(binary_data), 8):
|
143 |
+
if i + 8 <= len(binary_data):
|
144 |
+
byte = binary_data[i:i+8]
|
145 |
+
text += chr(int(byte, 2))
|
146 |
+
|
147 |
+
# Check for end marker
|
148 |
+
if "#####" in text:
|
149 |
+
# Extract content before marker
|
150 |
+
text = text.split("#####")[0]
|
151 |
+
try:
|
152 |
+
# Parse JSON
|
153 |
+
data = json.loads(text)
|
154 |
+
return json.dumps(data, ensure_ascii=False, indent=2)
|
155 |
+
except json.JSONDecodeError:
|
156 |
+
return text
|
157 |
+
break
|
|
|
|
|
158 |
|
159 |
return "Error: No valid watermark found"
|
160 |
|
161 |
except Exception as e:
|
162 |
return f"Error in decoding: {str(e)}"
|
163 |
|
164 |
+
|
165 |
def analyze_quality(self, original_path, watermarked_path):
|
166 |
"""Analyze watermark quality"""
|
167 |
try:
|