Python SDK
Official Python SDK for TruthMark invisible watermarking.
Installation
pip install truthmark-sdkQuick Example
from truthmark_sdk import TruthMarkClient
# Initialize
client = TruthMarkClient()
# Embed watermark
result = client.encode(
input_path="photo.jpg",
message="Copyright 2025 - My Company",
output_path="watermarked.jpg"
)
print(f"Quality: {result['psnr']:.2f} dB")
# Extract watermark
decoded = client.decode("watermarked.jpg")
if decoded['found']:
print(f"Message: {decoded['message']}")
print(f"Confidence: {decoded['confidence']*100:.1f}%")API Reference
TruthMarkClient()
Create a new TruthMark client instance.
client = TruthMarkClient()encode(input_path, message, output_path)
Embed an invisible watermark into an image.
Parameters:
input_path(str): Path to input imagemessage(str): Text to embed (max 500 chars)output_path(str): Where to save watermarked image
Returns:
{
"psnr": 42.5, # Quality metric
"bits_embedded": 256, # Bits embedded
"success": True
}decode(input_path)
Extract watermark from an image.
Parameters:
input_path(str): Path to watermarked image
Returns:
{
"found": True,
"message": "Extracted text",
"confidence": 0.85 # 0.0 to 1.0
}Advanced Examples
Batch Processing
import os
from pathlib import Path
client = TruthMarkClient()
# Process all images in a directory
input_dir = Path("./images")
output_dir = Path("./watermarked")
output_dir.mkdir(exist_ok=True)
for img_path in input_dir.glob("*.png"):
output_path = output_dir / img_path.name
result = client.encode(
str(img_path),
f"Copyright 2025 - Image: {img_path.name}",
str(output_path)
)
print(f"✓ {img_path.name}: PSNR={result['psnr']:.1f}dB")JSON Metadata
import json
# Embed structured data
metadata = {
"author": "John Doe",
"created": "2025-01-15",
"license": "CC-BY-4.0",
"ai_model": "DALL-E-3"
}
client.encode(
"ai_art.png",
json.dumps(metadata),
"tagged.png"
)
# Extract and parse
result = client.decode("tagged.png")
if result['found']:
data = json.loads(result['message'])
print(f"Author: {data['author']}")
print(f"AI Model: {data['ai_model']}")Verification Function
def verify_watermark(image_path, expected_message, min_confidence=0.7):
"""Verify image contains expected watermark."""
result = client.decode(image_path)
return (
result['found'] and
result['message'] == expected_message and
result['confidence'] >= min_confidence
)
# Usage
if verify_watermark("image.png", "Copyright 2025", min_confidence=0.8):
print("✓ Watermark verified")
else:
print("✗ Verification failed")Best Practices
✅ Keep messages concise
Messages under 200 characters encode better
✅ Check confidence scores
Only trust decoded messages with confidence > 0.7
✅ Use JSON for structured data
Embed multiple fields using JSON.dumps()
❌ Don't use tiny images
Minimum recommended size: 512x512 pixels