GameIndustry.eu /  Blog /
EnglishPython - Delete Metadata


Python - Delete Metadata

Eingetragen: 25.08.2023

python
Python Script: A small Python script for removing metadata in images. Various image formats are taken into account.

After a run, there is a summary of how much space was saved by removing the metadata.



An existing installation of Python is required. Before running the script, make sure that you have installed the required libraries.

Attention. The files are irrevocably overwritten. If you are not sure, create a backup before.

 pip install Pillow

 import os
from PIL import Image

def remove_metadata(image_path):
try:
image = Image.open(image_path)

Before running the script, make sure that you have installed the required libraries
image_without_metadata = Image.new(image.mode, image.size)
image_without_metadata.putdata(list(image.getdata()))

# Save the image without metadata
image_without_metadata.save(image_path)
return True
except Exception as e:
print(f"Error removing metadata from {image_path}: {e}")
return False

def process_folder(folder_path):
total_removed_bytes = 0

for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(('.jpg', '.png', '.gif', '.svg', '.bmp', '.tiff', '.webp')):
image_path = os.path.join(root, file)
original_size = os.path.getsize(image_path)

if remove_metadata(image_path):
new_size = os.path.getsize(image_path)
removed_bytes = original_size - new_size
total_removed_bytes += removed_bytes
# Optional screen output
print(f"Metadaten von {image_path} entfernt.")

total_removed_mb = total_removed_bytes / (1024 * 1024)
# Optional screen output
print(f"Gesamt entfernte Metadaten: {total_removed_mb:.2f} Megabyte")

if __name__ == "__main__":
folder_path = r"C:\Pictures" # Customize Path
process_folder(folder_path)

 

 

  Rules for posting comments can be found in the F.A.Q.