mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-06 03:05:25 +00:00
fix: bad way to overwrite exif for webp
This commit is contained in:
parent
a27145e0e0
commit
d30307c824
@ -2,27 +2,23 @@ from PIL import Image
|
|||||||
import piexif
|
import piexif
|
||||||
from PIL.PngImagePlugin import PngInfo
|
from PIL.PngImagePlugin import PngInfo
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
def write_image_metadata(image_path, metadata):
|
def write_image_metadata(image_path, metadata):
|
||||||
img = Image.open(image_path)
|
img = Image.open(image_path)
|
||||||
image_path_str = str(image_path)
|
image_path_str = str(image_path)
|
||||||
|
|
||||||
if image_path_str.lower().endswith((".jpg", ".jpeg", ".tiff")):
|
if image_path_str.lower().endswith((".jpg", ".jpeg", ".tiff", ".webp")):
|
||||||
exif_dict = piexif.load(image_path)
|
exif_dict = {"0th": {}, "Exif": {}, "GPS": {}, "1st": {}, "thumbnail": None}
|
||||||
updated_description = json.dumps(metadata).encode("utf-8")
|
exif_dict["0th"][piexif.ImageIFD.ImageDescription] = json.dumps(
|
||||||
exif_dict["0th"][piexif.ImageIFD.ImageDescription] = updated_description
|
metadata
|
||||||
|
).encode("utf-8")
|
||||||
exif_bytes = piexif.dump(exif_dict)
|
exif_bytes = piexif.dump(exif_dict)
|
||||||
img.save(image_path, exif=exif_bytes)
|
img.save(image_path, exif=exif_bytes)
|
||||||
elif image_path_str.lower().endswith(".png"):
|
elif image_path_str.lower().endswith(".png"):
|
||||||
metadata_info = PngInfo()
|
metadata_info = PngInfo()
|
||||||
metadata_info.add_text("Description", json.dumps(metadata))
|
metadata_info.add_text("Description", json.dumps(metadata))
|
||||||
img.save(image_path, "PNG", pnginfo=metadata_info)
|
img.save(image_path, "PNG", pnginfo=metadata_info)
|
||||||
elif image_path_str.lower().endswith(".webp"):
|
|
||||||
# Convert metadata to bytes
|
|
||||||
metadata_bytes = json.dumps(metadata).encode('utf-8')
|
|
||||||
# Use exif parameter to save metadata
|
|
||||||
img.save(image_path, "WebP", exif=metadata_bytes, quality=85)
|
|
||||||
else:
|
else:
|
||||||
print(f"Skipping unsupported file format: {image_path_str}")
|
print(f"Skipping unsupported file format: {image_path_str}")
|
||||||
|
|
||||||
@ -31,32 +27,23 @@ def get_image_metadata(image_path):
|
|||||||
img = Image.open(image_path)
|
img = Image.open(image_path)
|
||||||
image_path_str = str(image_path)
|
image_path_str = str(image_path)
|
||||||
|
|
||||||
if image_path_str.lower().endswith((".jpg", ".jpeg", ".tiff")):
|
if image_path_str.lower().endswith((".jpg", ".jpeg", ".tiff", ".webp")):
|
||||||
exif_dict = piexif.load(str(image_path)) # Convert Path object to string
|
|
||||||
existing_description = exif_dict["0th"].get(
|
|
||||||
piexif.ImageIFD.ImageDescription, b"{}"
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
|
exif_dict = piexif.load(image_path_str)
|
||||||
|
existing_description = exif_dict["0th"].get(
|
||||||
|
piexif.ImageIFD.ImageDescription, b"{}"
|
||||||
|
)
|
||||||
return json.loads(existing_description.decode("utf-8"))
|
return json.loads(existing_description.decode("utf-8"))
|
||||||
except json.JSONDecodeError as e:
|
except Exception as e:
|
||||||
print(f"Error decoding JSON metadata for {image_path_str}: {e}")
|
print(f"Error decoding EXIF metadata for {image_path_str}: {e}")
|
||||||
return {}
|
return None
|
||||||
elif image_path_str.lower().endswith(".png"):
|
elif image_path_str.lower().endswith(".png"):
|
||||||
existing_description = img.info.get("Description", "{}")
|
existing_description = img.info.get("Description", "{}")
|
||||||
try:
|
try:
|
||||||
return json.loads(existing_description)
|
return json.loads(existing_description)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
print(f"Error decoding JSON metadata for {image_path_str}: {e}")
|
print(f"Error decoding PNG metadata for {image_path_str}: {e}")
|
||||||
return {}
|
return None
|
||||||
elif image_path_str.lower().endswith(".webp"):
|
|
||||||
existing_metadata = img.info.get("exif", b"{}")
|
|
||||||
try:
|
|
||||||
if isinstance(existing_metadata, bytes):
|
|
||||||
existing_metadata = existing_metadata.decode('utf-8')
|
|
||||||
return json.loads(existing_metadata)
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
print(f"Error decoding JSON metadata for {image_path_str}: {e}")
|
|
||||||
return {}
|
|
||||||
else:
|
else:
|
||||||
print(f"Unsupported file format: {image_path_str}")
|
print(f"Unsupported file format: {image_path_str}")
|
||||||
return None
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user