-
Notifications
You must be signed in to change notification settings - Fork 5
/
replace_metadata.py
39 lines (25 loc) · 1.18 KB
/
replace_metadata.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
#!/usr/bin/env python3
import argparse
from PIL import Image
from PIL.PngImagePlugin import PngInfo
def extract_metadata(image_path):
image = Image.open(image_path)
metadata = image.info
return metadata
def replace_metadata(source_image_path, target_image_path, output_image_path):
metadata = extract_metadata(source_image_path)
target_image = Image.open(target_image_path)
png_info = PngInfo()
for key, value in metadata.items():
png_info.add_text(key, str(value))
target_image.save(output_image_path, pnginfo=png_info)
def main():
parser = argparse.ArgumentParser(description="Copy metadata from one PNG image to another.")
parser.add_argument('source', type=str, help="Path to the source PNG image with the metadata.")
parser.add_argument('target', type=str, help="Path to the target PNG image to replace metadata.")
parser.add_argument('output', type=str, help="Path for the output PNG image with replaced metadata.")
args = parser.parse_args()
replace_metadata(args.source, args.target, args.output)
print(f"Metadata from '{args.source}' has been copied to '{args.output}'.")
if __name__ == "__main__":
main()