代码如下:(保存为.py文件)
import os
from PIL import Image
def remove_exif_from_image(image_path, output_path):
try:
image = Image.open(image_path)
# Remove EXIF data by saving the image without preserving it
data = list(image.getdata())
image_no_exif = Image.new(image.mode, image.size)
image_no_exif.putdata(data)
# Save the new image without EXIF data
image_no_exif.save(output_path)
except Exception as e:
print(f"Error processing {image_path}: {e}")
def remove_exif_from_folder(input_folder, output_folder):
# Ensure the output folder exists
os.makedirs(output_folder, exist_ok=True)
# Iterate over all files in the input folder
for filename in os.listdir(input_folder):
input_path = os.path.join(input_folder, filename)
# Process only image files
if os.path.isfile(input_path) and filename.lower().endswith(('jpg', 'jpeg', 'png', 'tiff')):
output_path = os.path.join(output_folder, filename)
remove_exif_from_image(input_path, output_path)
print(f"Processed: {filename}")
# Example usage:
input_folder_path = r'C:\Users\YourUsername\Pictures\InputFolder' # 输入文件夹路径
output_folder_path = r'C:\Users\YourUsername\Pictures\OutputFolder' # 输出文件夹路径
remove_exif_from_folder(input_folder_path, output_folder_path)
站长使用Pycharm运行,需要安装PIL和image
PIL是一个用于图像处理的第三方库
Image
是 PIL
(或 Pillow
)库中的一个模块
导入方式:可以通过以下命令来安装
from PIL import Image
pip install Pillow
不会用的话,不用去尝试,以免徒增烦恼
解释:图片的EXIF数据
记录数码照片的属性信息和拍摄数据,去除后,可以节省空间
优化后的一版
上面的代码只能对文件夹下的内容进行去除,这一版可以对文件夹下的文件夹内容进行去除
import os
from PIL import Image
def remove_exif_from_image(image_path, output_path):
try:
image = Image.open(image_path)
# Remove EXIF data by saving the image without preserving it
data = list(image.getdata())
image_no_exif = Image.new(image.mode, image.size)
image_no_exif.putdata(data)
# Save the new image without EXIF data
image_no_exif.save(output_path)
except Exception as e:
print(f"Error processing {image_path}: {e}")
def remove_exif_from_folder(input_folder, output_folder):
# Iterate over all files and subfolders in the input folder
for root, dirs, files in os.walk(input_folder):
# Determine the corresponding output path
relative_path = os.path.relpath(root, input_folder)
output_dir = os.path.join(output_folder, relative_path)
# Ensure the output folder exists
os.makedirs(output_dir, exist_ok=True)
# Process image files in the current directory
for filename in files:
input_path = os.path.join(root, filename)
# Process only image files
if filename.lower().endswith(('jpg', 'jpeg', 'png', 'tiff')):
output_path = os.path.join(output_dir, filename)
remove_exif_from_image(input_path, output_path)
print(f"Processed: {input_path} -> {output_path}")
# Example usage:
input_folder_path = r'C:\Users\198\Desktop\1' # 输入文件夹路径
output_folder_path = r'C:\Users\198\Desktop\2' # 输出文件夹路径
remove_exif_from_folder(input_folder_path, output_folder_path)
© 版权声明
THE END
相关推荐