Spaces:
Sleeping
Sleeping
File size: 7,681 Bytes
ae3e8a6 6528fbf b2358ad ae3e8a6 aa56dd1 ae3e8a6 6eb0ffe 577e2d7 6eb0ffe a12f164 6eb0ffe 577e2d7 a12f164 6eb0ffe a12f164 6eb0ffe a12f164 6eb0ffe |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# utils.py
import cv2
import numpy as np
from PIL import Image, PngImagePlugin, ImageDraw
import json
from datetime import datetime
from cryptography.fernet import Fernet
import base64
import hashlib
class WatermarkProcessor:
def __init__(self, encryption_key=None):
"""Initialize with optional encryption key"""
if encryption_key:
self.fernet = Fernet(encryption_key)
else:
key = Fernet.generate_key()
self.fernet = Fernet(key)
def to_bin(self, data):
"""Convert data to binary format as string"""
if isinstance(data, str):
return ''.join(format(ord(char), '08b') for char in data)
elif isinstance(data, bytes):
return ''.join(format(x, '08b') for x in data)
elif isinstance(data, np.ndarray):
return [format(i, "08b") for i in data]
elif isinstance(data, int) or isinstance(data, np.uint8):
return format(data, "08b")
else:
raise TypeError("Type not supported.")
def create_preview(self, image_path, watermark_text, opacity=0.3):
"""Create a preview of watermark on image"""
try:
image = Image.open(image_path)
txt_layer = Image.new('RGBA', image.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(txt_layer)
# Calculate text position
text_width = draw.textlength(watermark_text)
text_x = (image.width - text_width) // 2
text_y = image.height // 2
# Add watermark text
draw.text((text_x, text_y), watermark_text,
fill=(255, 255, 255, int(255 * opacity)))
# Combine layers
preview = Image.alpha_composite(image.convert('RGBA'), txt_layer)
return preview
except Exception as e:
return None
def png_encode(self, im_name, extra):
"""Encode watermark using PNG metadata"""
try:
im = Image.open(im_name)
info = PngImagePlugin.PngInfo()
info.add_text("TXT", extra)
im.save("test.png", pnginfo=info)
return "test.png", "Watermark added successfully"
except Exception as e:
return im_name, f"Error adding watermark: {str(e)}"
def encode(self, image_path, watermark_text, metadata=None):
"""Encode watermark using simple LSB steganography"""
try:
image = cv2.imread(image_path)
if image is None:
raise ValueError("Could not read image file")
# Prepare watermark data
data = {
'text': watermark_text,
'timestamp': datetime.now().isoformat(),
'metadata': metadata or {}
}
# Convert to string and add delimiter
secret_data = json.dumps(data, ensure_ascii=False) + "###END###"
# Convert to binary string
binary_secret = ''.join(format(ord(char), '08b') for char in secret_data)
# Check capacity
if len(binary_secret) > image.shape[0] * image.shape[1] * 3:
return image_path, "Error: Image too small for watermark data"
# Embed data
data_index = 0
for i in range(image.shape[0]):
for j in range(image.shape[1]):
for k in range(3):
if data_index < len(binary_secret):
# Get current pixel value
pixel = image[i, j, k]
# Clear the LSB by setting it to 0 and add the watermark bit
pixel = (pixel & 0xFE) | (int(binary_secret[data_index]) & 0x01)
# Ensure value stays within uint8 range
image[i, j, k] = np.uint8(pixel)
data_index += 1
else:
break
if data_index >= len(binary_secret):
break
if data_index >= len(binary_secret):
break
# Save result
output_path = f"watermarked_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
cv2.imwrite(output_path, image)
return output_path, "Watermark added successfully"
except Exception as e:
return image_path, f"Error in encoding: {str(e)}"
def decode(self, image_path):
"""Decode watermark using simple LSB steganography"""
try:
# Try PNG metadata method first
try:
im = Image.open(image_path)
if "TXT" in im.info:
return im.info["TXT"]
except:
pass
# Read image
image = cv2.imread(image_path)
if image is None:
raise ValueError("Could not read image file")
# Extract LSB data
binary_text = ''
# Get LSB from each byte of each pixel
for i in range(image.shape[0]):
for j in range(image.shape[1]):
for k in range(3):
bit = image[i, j, k] & 1
binary_text += str(bit)
# Convert binary to text
text = ''
# Process 8 bits at a time
for i in range(0, len(binary_text)-8, 8):
byte = binary_text[i:i+8]
text += chr(int(byte, 2))
# Check for end marker
if text[-7:] == "###END###":
extracted_text = text[:-7]
try:
# Attempt to parse as JSON
data = json.loads(extracted_text)
return json.dumps(data, ensure_ascii=False, indent=2)
except:
# If not valid JSON, return as plain text
return extracted_text.strip()
# If we got here, try to find any valid JSON in the text
try:
# Convert remaining bits
for i in range(0, len(binary_text), 8):
if i + 8 <= len(binary_text):
byte = binary_text[i:i+8]
text += chr(int(byte, 2))
# Look for JSON markers
start = text.find('{')
end = text.rfind('}')
if start != -1 and end != -1:
json_text = text[start:end+1]
try:
data = json.loads(json_text)
return json.dumps(data, ensure_ascii=False, indent=2)
except:
pass
except:
pass
return text if text.strip() else "Error: No valid watermark found"
except Exception as e:
return f"Error in decoding: {str(e)}"
|