Add EXIF Editor, consolidate channel key resolution
EXIF Editor (Library → CLI → API → WebUI): - src/stegasoo/utils.py: read_image_exif(), write_image_exif() - CLI: stegasoo tools exif [--clear|--set Field=Value] - API: /api/tools/exif, /api/tools/exif/update, /api/tools/exif/clear - WebUI: EXIF Editor tab with inline editing, clear all, save/download Architectural consolidation: - Moved resolve_channel_key() to src/stegasoo/channel.py (was duplicated in 3 frontends) - Added get_channel_response_info() for consistent API/WebUI responses - Frontends now use thin wrappers that translate exceptions DCT improvements: - Added will_fit_by_mode() pre-check to WebUI encode (fail fast) - Suggests LSB mode when DCT capacity exceeded Dependencies: - Added piexif>=1.1.0 for EXIF editing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -372,6 +372,124 @@ def has_channel_key() -> bool:
|
||||
return get_channel_key() is not None
|
||||
|
||||
|
||||
def resolve_channel_key(
|
||||
value: str | None = None,
|
||||
*,
|
||||
file_path: str | Path | None = None,
|
||||
no_channel: bool = False,
|
||||
) -> str | None:
|
||||
"""
|
||||
Resolve a channel key from user input (unified for all frontends).
|
||||
|
||||
This consolidates channel key resolution logic used by CLI, API, and WebUI.
|
||||
|
||||
Args:
|
||||
value: Input value:
|
||||
- 'auto' or None: Use server-configured key
|
||||
- 'none' or '': Public mode (no channel key)
|
||||
- explicit key: Validate and use
|
||||
file_path: Path to file containing channel key
|
||||
no_channel: If True, return "" for public mode (overrides value)
|
||||
|
||||
Returns:
|
||||
None: Use server-configured key (auto mode)
|
||||
"": Public mode (no channel key)
|
||||
str: Explicit valid channel key
|
||||
|
||||
Raises:
|
||||
ValueError: If key format is invalid
|
||||
FileNotFoundError: If file_path doesn't exist
|
||||
|
||||
Example:
|
||||
>>> resolve_channel_key("auto") # -> None
|
||||
>>> resolve_channel_key("none") # -> ""
|
||||
>>> resolve_channel_key(no_channel=True) # -> ""
|
||||
>>> resolve_channel_key("ABCD-1234-...") # -> "ABCD-1234-..."
|
||||
>>> resolve_channel_key(file_path="key.txt") # reads from file
|
||||
"""
|
||||
debug.print(f"resolve_channel_key: value={value}, file_path={file_path}, no_channel={no_channel}")
|
||||
|
||||
# no_channel flag takes precedence
|
||||
if no_channel:
|
||||
debug.print("resolve_channel_key: public mode (no_channel=True)")
|
||||
return ""
|
||||
|
||||
# Read from file if provided
|
||||
if file_path:
|
||||
path = Path(file_path)
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(f"Channel key file not found: {file_path}")
|
||||
key = path.read_text().strip()
|
||||
if not validate_channel_key(key):
|
||||
raise ValueError(f"Invalid channel key format in file: {file_path}")
|
||||
debug.print(f"resolve_channel_key: from file -> {get_channel_fingerprint(key)}")
|
||||
return format_channel_key(key)
|
||||
|
||||
# Handle value string
|
||||
if value is None or value.lower() == "auto":
|
||||
debug.print("resolve_channel_key: auto mode (server config)")
|
||||
return None
|
||||
|
||||
if value == "" or value.lower() == "none":
|
||||
debug.print("resolve_channel_key: public mode (explicit none)")
|
||||
return ""
|
||||
|
||||
# Explicit key - validate
|
||||
if validate_channel_key(value):
|
||||
formatted = format_channel_key(value)
|
||||
debug.print(f"resolve_channel_key: explicit key -> {get_channel_fingerprint(formatted)}")
|
||||
return formatted
|
||||
|
||||
raise ValueError(
|
||||
"Invalid channel key format. Expected: XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX\n"
|
||||
"Generate a new key with: stegasoo channel generate"
|
||||
)
|
||||
|
||||
|
||||
def get_channel_response_info(channel_key: str | None) -> dict:
|
||||
"""
|
||||
Get channel info for API/WebUI responses.
|
||||
|
||||
Args:
|
||||
channel_key: Resolved channel key (None=auto, ""=public, str=explicit)
|
||||
|
||||
Returns:
|
||||
Dict with mode, fingerprint, and display info
|
||||
|
||||
Example:
|
||||
>>> info = get_channel_response_info("ABCD-1234-...")
|
||||
>>> info['mode']
|
||||
'explicit'
|
||||
"""
|
||||
if channel_key is None:
|
||||
# Auto mode - check server config
|
||||
server_key = get_channel_key()
|
||||
if server_key:
|
||||
return {
|
||||
"mode": "private",
|
||||
"fingerprint": get_channel_fingerprint(server_key),
|
||||
"source": "server",
|
||||
}
|
||||
return {
|
||||
"mode": "public",
|
||||
"fingerprint": None,
|
||||
"source": "server",
|
||||
}
|
||||
|
||||
if channel_key == "":
|
||||
return {
|
||||
"mode": "public",
|
||||
"fingerprint": None,
|
||||
"source": "explicit",
|
||||
}
|
||||
|
||||
return {
|
||||
"mode": "private",
|
||||
"fingerprint": get_channel_fingerprint(channel_key),
|
||||
"source": "explicit",
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# CLI SUPPORT
|
||||
# =============================================================================
|
||||
|
||||
@@ -887,6 +887,83 @@ def tools_peek(image, as_json):
|
||||
click.echo()
|
||||
|
||||
|
||||
@tools.command("exif")
|
||||
@click.argument("image", type=click.Path(exists=True))
|
||||
@click.option("--clear", is_flag=True, help="Remove all EXIF metadata")
|
||||
@click.option("--set", "set_fields", multiple=True, help="Set EXIF field (e.g. --set Artist=John)")
|
||||
@click.option("-o", "--output", type=click.Path(), help="Output file (required for modifications)")
|
||||
@click.option("--json", "as_json", is_flag=True, help="Output as JSON")
|
||||
def tools_exif(image, clear, set_fields, output, as_json):
|
||||
"""View or edit EXIF metadata.
|
||||
|
||||
Examples:
|
||||
|
||||
stegasoo tools exif photo.jpg
|
||||
|
||||
stegasoo tools exif photo.jpg --clear -o clean.jpg
|
||||
|
||||
stegasoo tools exif photo.jpg --set Artist="John Doe" -o updated.jpg
|
||||
"""
|
||||
from .utils import read_image_exif, strip_image_metadata, write_image_exif
|
||||
|
||||
with open(image, "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
# View mode (no modifications)
|
||||
if not clear and not set_fields:
|
||||
exif = read_image_exif(image_data)
|
||||
|
||||
if as_json:
|
||||
click.echo(json.dumps(exif, indent=2, default=str))
|
||||
else:
|
||||
click.echo(f"\n EXIF Metadata: {Path(image).name}")
|
||||
click.echo(f" {'─' * 45}")
|
||||
if not exif:
|
||||
click.echo(" No EXIF metadata found")
|
||||
else:
|
||||
for key, value in sorted(exif.items()):
|
||||
# Skip complex nested structures for display
|
||||
if isinstance(value, dict):
|
||||
click.echo(f" {key}: [complex data]")
|
||||
elif isinstance(value, list):
|
||||
click.echo(f" {key}: {value}")
|
||||
else:
|
||||
# Truncate long values
|
||||
str_val = str(value)
|
||||
if len(str_val) > 50:
|
||||
str_val = str_val[:47] + "..."
|
||||
click.echo(f" {key}: {str_val}")
|
||||
click.echo()
|
||||
return
|
||||
|
||||
# Modification mode - require output file
|
||||
if not output:
|
||||
raise click.UsageError("Output file required for modifications (use -o/--output)")
|
||||
|
||||
if clear:
|
||||
# Strip all metadata
|
||||
clean_data = strip_image_metadata(image_data, output_format="JPEG")
|
||||
with open(output, "wb") as f:
|
||||
f.write(clean_data)
|
||||
click.echo(f"Cleared EXIF metadata, saved to: {output}")
|
||||
elif set_fields:
|
||||
# Parse field=value pairs
|
||||
updates = {}
|
||||
for field in set_fields:
|
||||
if "=" not in field:
|
||||
raise click.UsageError(f"Invalid format: {field} (use Field=Value)")
|
||||
key, val = field.split("=", 1)
|
||||
updates[key.strip()] = val.strip()
|
||||
|
||||
try:
|
||||
updated_data = write_image_exif(image_data, updates)
|
||||
with open(output, "wb") as f:
|
||||
f.write(updated_data)
|
||||
click.echo(f"Updated {len(updates)} EXIF field(s), saved to: {output}")
|
||||
except ValueError as e:
|
||||
raise click.UsageError(str(e))
|
||||
|
||||
|
||||
def main():
|
||||
"""Entry point for CLI."""
|
||||
cli(obj={})
|
||||
|
||||
@@ -18,6 +18,159 @@ from .constants import DAY_NAMES
|
||||
from .debug import debug
|
||||
|
||||
|
||||
def read_image_exif(image_data: bytes) -> dict:
|
||||
"""
|
||||
Read EXIF metadata from an image.
|
||||
|
||||
Args:
|
||||
image_data: Raw image bytes
|
||||
|
||||
Returns:
|
||||
Dict with EXIF fields (tag names as keys)
|
||||
|
||||
Example:
|
||||
>>> exif = read_image_exif(photo_bytes)
|
||||
>>> print(exif.get('Make')) # Camera manufacturer
|
||||
"""
|
||||
from PIL.ExifTags import GPSTAGS, TAGS
|
||||
|
||||
result = {}
|
||||
|
||||
try:
|
||||
img = Image.open(io.BytesIO(image_data))
|
||||
exif_data = img._getexif()
|
||||
|
||||
if exif_data:
|
||||
for tag_id, value in exif_data.items():
|
||||
tag = TAGS.get(tag_id, str(tag_id))
|
||||
|
||||
# Handle GPS data specially
|
||||
if tag == "GPSInfo" and isinstance(value, dict):
|
||||
gps = {}
|
||||
for gps_tag_id, gps_value in value.items():
|
||||
gps_tag = GPSTAGS.get(gps_tag_id, str(gps_tag_id))
|
||||
# Convert tuples/IFDRational to simple types
|
||||
if hasattr(gps_value, "numerator"):
|
||||
gps[gps_tag] = float(gps_value)
|
||||
elif isinstance(gps_value, tuple):
|
||||
gps[gps_tag] = [
|
||||
float(v) if hasattr(v, "numerator") else v
|
||||
for v in gps_value
|
||||
]
|
||||
else:
|
||||
gps[gps_tag] = gps_value
|
||||
result[tag] = gps
|
||||
# Convert IFDRational to float
|
||||
elif hasattr(value, "numerator"):
|
||||
result[tag] = float(value)
|
||||
# Convert bytes to string if possible
|
||||
elif isinstance(value, bytes):
|
||||
try:
|
||||
result[tag] = value.decode("utf-8", errors="replace").strip("\x00")
|
||||
except Exception:
|
||||
result[tag] = f"<{len(value)} bytes>"
|
||||
# Handle tuples of IFDRational
|
||||
elif isinstance(value, tuple) and value and hasattr(value[0], "numerator"):
|
||||
result[tag] = [float(v) for v in value]
|
||||
else:
|
||||
result[tag] = value
|
||||
|
||||
img.close()
|
||||
except Exception as e:
|
||||
debug.print(f"Error reading EXIF: {e}")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def write_image_exif(image_data: bytes, exif_updates: dict) -> bytes:
|
||||
"""
|
||||
Write/update EXIF metadata in a JPEG image.
|
||||
|
||||
Args:
|
||||
image_data: Raw JPEG image bytes
|
||||
exif_updates: Dict of EXIF fields to update (tag names as keys)
|
||||
Use None as value to delete a field
|
||||
|
||||
Returns:
|
||||
Image bytes with updated EXIF
|
||||
|
||||
Raises:
|
||||
ValueError: If image is not JPEG or piexif not available
|
||||
|
||||
Example:
|
||||
>>> updated = write_image_exif(jpeg_bytes, {"Artist": "John Doe"})
|
||||
"""
|
||||
try:
|
||||
import piexif
|
||||
except ImportError:
|
||||
raise ValueError("piexif required for EXIF editing: pip install piexif")
|
||||
|
||||
# Verify it's a JPEG
|
||||
if not image_data[:2] == b"\xff\xd8":
|
||||
raise ValueError("EXIF editing only supported for JPEG images")
|
||||
|
||||
debug.print(f"Writing EXIF updates: {list(exif_updates.keys())}")
|
||||
|
||||
# Load existing EXIF
|
||||
try:
|
||||
exif_dict = piexif.load(image_data)
|
||||
except Exception:
|
||||
# No existing EXIF, start fresh
|
||||
exif_dict = {"0th": {}, "Exif": {}, "GPS": {}, "1st": {}, "thumbnail": None}
|
||||
|
||||
# Map common tag names to piexif IFD and tag IDs
|
||||
tag_mapping = {
|
||||
# 0th IFD (main image)
|
||||
"Make": (piexif.ImageIFD.Make, "0th"),
|
||||
"Model": (piexif.ImageIFD.Model, "0th"),
|
||||
"Software": (piexif.ImageIFD.Software, "0th"),
|
||||
"Artist": (piexif.ImageIFD.Artist, "0th"),
|
||||
"Copyright": (piexif.ImageIFD.Copyright, "0th"),
|
||||
"ImageDescription": (piexif.ImageIFD.ImageDescription, "0th"),
|
||||
"DateTime": (piexif.ImageIFD.DateTime, "0th"),
|
||||
"Orientation": (piexif.ImageIFD.Orientation, "0th"),
|
||||
# Exif IFD
|
||||
"DateTimeOriginal": (piexif.ExifIFD.DateTimeOriginal, "Exif"),
|
||||
"DateTimeDigitized": (piexif.ExifIFD.DateTimeDigitized, "Exif"),
|
||||
"UserComment": (piexif.ExifIFD.UserComment, "Exif"),
|
||||
"ExposureTime": (piexif.ExifIFD.ExposureTime, "Exif"),
|
||||
"FNumber": (piexif.ExifIFD.FNumber, "Exif"),
|
||||
"ISOSpeedRatings": (piexif.ExifIFD.ISOSpeedRatings, "Exif"),
|
||||
"FocalLength": (piexif.ExifIFD.FocalLength, "Exif"),
|
||||
"LensMake": (piexif.ExifIFD.LensMake, "Exif"),
|
||||
"LensModel": (piexif.ExifIFD.LensModel, "Exif"),
|
||||
}
|
||||
|
||||
for tag_name, value in exif_updates.items():
|
||||
if tag_name not in tag_mapping:
|
||||
debug.print(f"Unknown EXIF tag: {tag_name}, skipping")
|
||||
continue
|
||||
|
||||
tag_id, ifd = tag_mapping[tag_name]
|
||||
|
||||
if value is None:
|
||||
# Delete the tag
|
||||
if tag_id in exif_dict[ifd]:
|
||||
del exif_dict[ifd][tag_id]
|
||||
debug.print(f"Deleted EXIF tag: {tag_name}")
|
||||
else:
|
||||
# Set the tag (encode strings as bytes)
|
||||
if isinstance(value, str):
|
||||
value = value.encode("utf-8")
|
||||
exif_dict[ifd][tag_id] = value
|
||||
debug.print(f"Set EXIF tag: {tag_name}")
|
||||
|
||||
# Serialize EXIF and insert into image
|
||||
exif_bytes = piexif.dump(exif_dict)
|
||||
output = io.BytesIO()
|
||||
img = Image.open(io.BytesIO(image_data))
|
||||
img.save(output, "JPEG", exif=exif_bytes, quality=95)
|
||||
output.seek(0)
|
||||
|
||||
debug.print(f"EXIF updated: {len(image_data)} -> {len(output.getvalue())} bytes")
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def strip_image_metadata(image_data: bytes, output_format: str = "PNG") -> bytes:
|
||||
"""
|
||||
Remove all metadata (EXIF, ICC profiles, etc.) from an image.
|
||||
|
||||
Reference in New Issue
Block a user