Want to code faster? Our Python Code Generator lets you create Python scripts with just a few clicks. Try it now!
In this tutorial, you will learn how to delete metadata from an image with Python. Metadata in an image refers to the additional information embedded within the file, providing details about the image's creation and other relevant properties. This information can include details such as the camera settings, date and time of capture, location, device name, and sometimes even the photographer's name. Metadata enhances the understanding and organization of digital images.
Deleting metadata from an image is done to protect privacy, reduce file size, enhance security, maintain anonymity, and achieve a clean and simple appearance. We like security, so of course, that's our major concern. By deleting the metadata, we remove pieces of information that can be useful to attackers in attempts to compromise our security.
For this program, we'll use Pillow
(not the one you sleep on, of course).
In Python, Pillow
is a powerful image processing library that provides functionalities for opening, manipulating, and saving various image file formats. The manipulation includes deleting metadata from images.
We’re going to achieve the deletion of the metadata from images by utilizing the Pillow
library, which will clear the metadata from the specified image by reading the image data excluding metadata, creating a new image without metadata, and saving it over the original file. Pretty straightforward.
Now, let’s do that in Python. First things first, we install the necessary libraries:
$ pip install pillow
argparse
is a Python module used to parse command-line arguments and generate user-friendly command-line interfaces. You can check our tutorial on that.
Before we get into our code, let me show you the existing metadata on a given image. If you do not know how to extract metadata from a given image, check this tutorial out.
We can see that the given image, which is me.jpg
, contains a lot of metadata. This includes the make and model of the device that took the picture. This is Apple’s iPhone X. From this image’s metadata, we already know what device was used to take the picture. This information alone can go a long way in social engineering a target. With that information, attackers can also check for known vulnerabilities attributed to the device to exploit the target (if any).
Also, we see the date and time the image was taken and even the software that was used to take the picture, which is Instagram. I don’t know about you, but if you care about privacy like me, you'll agree with me that that’s way too much information from an image.
We are going to get rid of all that data in a second. Remember that parameters like filename, image size, image height, image width, and so on are not considered metadata. This is basic information every image must have. Every image has a name and a dimension. Nothing special. The other parameters, like the make, model, date, etc are our major concern.
Let’s get coding. Open up a file, name it meaningfully like, clear_metadata.py
We start by importing the necessary libraries:
# Import necessary libraries.
import argparse
from PIL import Image
Next, we create our function that clears metadata from a given imgname
:
# Function to clear Metadata from a specified image.
def clear_all_metadata(imgname):
# Open the image file
img = Image.open(imgname)
# 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 = Image.new(img.mode, img.size)
img_without_metadata.putdata(data)
# Save the new image over the original file, effectively removing metadata.
img_without_metadata.save(imgname)
print(f"Metadata successfully cleared from '{imgname}'.")
This function takes the filename of an image, opens it using Pillow
, removes the metadata by creating a new image with the same characteristics but without metadata, and then saves the new image over the original file, ultimately clearing the metadata. Quite simple.
Finally, we handle user arguments using the mighty argparse
:
# 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:
clear_all_metadata(args.img)
We could use the sys module as it's a single argument, but let’s just run our code:
$ python clear_metadata.py me.JPG
Result:
Great. Now, let’s confirm by extracting the metadata using this program:
Now, we see that the useful information (metadata), such as model, make, date, etc, are no longer there. It’s just basic, easily obtainable information left. This means we have successfully deleted/cleared the metadata from our image.
Another very important note to take is that different images have different metadata. So feel free to test with different images (Ethically, of course).
Learn also: How to Compress Images in Python.
I hope you enjoyed this one. Till next time, happy coding ♥
Ready for more? Dive deeper into coding with our AI-powered Code Explainer. Don't miss it!
View Full Code Assist My Coding
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!