-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpng_info_copy.py
47 lines (39 loc) · 1.49 KB
/
png_info_copy.py
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
import sys
import argparse
from PIL import Image
from PIL.PngImagePlugin import PngInfo
def copy_metadata(src_img, tgt_img):
try:
src_image = Image.open(src_img)
src_info = src_image.text
except Exception as e:
print(f"Error reading metadata from {src_img}: {e}")
return
try:
tgt_image = Image.open(tgt_img)
except Exception as e:
print(f"Error opening target image {tgt_img}: {e}")
return
tgt_info = PngInfo()
for k, v in src_info.items():
if isinstance(v, str):
try:
v = v.encode("utf-8")
except UnicodeEncodeError:
# This is a placeholder for handling different encodings
# or logging an issue if the encoding fails
pass
tgt_info.add_text(k, v)
try:
tgt_image.save(tgt_img, format=tgt_image.format, pnginfo=tgt_info)
print(f"Metadata successfully copied from {src_img} to {tgt_img}")
except Exception as e:
print(f"Error writing metadata to {tgt_img}: {e}")
def main():
parser = argparse.ArgumentParser(description="Copy metadata from one PNG image to another.")
parser.add_argument("--metadata-source", required=True, help="Source image with metadata")
parser.add_argument("--metadata-target", required=True, help="Target image to copy metadata to")
args = parser.parse_args()
copy_metadata(args.metadata_source, args.metadata_target)
if __name__ == "__main__":
main()