-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_metadata.py
34 lines (28 loc) · 1.16 KB
/
remove_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
import os
from PIL import Image
def remove_metadata(image_path):
try:
# Open the image
img = Image.open(image_path)
# Convert the image to remove metadata (EXIF)
data = list(img.getdata())
img_no_metadata = Image.new(img.mode, img.size)
img_no_metadata.putdata(data)
# Save the image, overwriting the original file
img_no_metadata.save(image_path)
print(f"Metadata removed and image saved: {image_path}")
except Exception as e:
print(f"Error processing image {image_path}: {e}")
def process_images(folder_path):
# Walk through the folder and all subfolders
for root, dirs, files in os.walk(folder_path):
for file in files:
# Check if the file is an image by file extension
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp')):
image_path = os.path.join(root, file)
remove_metadata(image_path)
if __name__ == "__main__":
# Get the current directory
current_directory = os.getcwd()
# Process all images in the current directory and subfolders
process_images(current_directory)