Code for How to Remove Metadata from Images in Python Tutorial

View on Github

# Import necessary libraries.
import argparse
from PIL import Image

# Function to clear Metadata from a specified image.
def clear_all_metadata(imgname):
    # Open the image file
    img =
    # Read the image data, excluding metadata.
    data = list(img.getdata())
    # Create a new image with the same mode and size but without metadata.
    img_without_metadata =, img.size)
    # Save the new image over the original file, effectively removing metadata.
    print(f"Metadata successfully cleared from '{imgname}'.")

# Setup command line argument parsing
parser = argparse.ArgumentParser(description="Remove metadata from an image file.")
parser.add_argument("img", help="Image file from which to remove metadata")

# Parse arguments
args = parser.parse_args()

# If an image file is provided, clear its metadata
if args.img: