![[站长原创]Python脚本一键去除图片的EXIF数据信息(节省空间)](https://yizhi2024.top/wp-content/uploads/2024/08/20240818182622651-学社山中人_2024-08-18_18-20-45.webp)
代码如下:(保存为.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:UsersYourUsernamePicturesInputFolder' # 输入文件夹路径
output_folder_path = r'C:UsersYourUsernamePicturesOutputFolder' # 输出文件夹路径
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:Users198Desktop1' # 输入文件夹路径
output_folder_path = r'C:Users198Desktop2' # 输出文件夹路径
remove_exif_from_folder(input_folder_path, output_folder_path)
标题带有亲测,是经过一定测试的源码或工具;破解软件或绿色软件一般都会报毒,如果您害怕,请勿下载安装,感谢
© 版权声明
THE END
相关推荐



![[亲测可用]如何获取百度网盘登陆Cookie?-学社山中人](https://yizhi2024.top/wp-content/uploads/2025/02/20250207020946449-学社山中人_2025-02-07_02-09-33.webp)

![[亲测可用]Python安装软件包时遇到AttributeError: module ‘pkgutil’ has no attribute ‘ImpImporter’. Did you mean: ‘zipimporter’?-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/08/20240818180716387-未命名2.webp)
![[亲测可用]IDM6.42免费安装已激活pj版-重新打包版(Windows软件)-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/09/20240925213046592-学社山中人_2024-09-25_21-30-24.webp)
![[亲测搭建可用]美女写真打赏视频打赏系统2024.3.31(带有视频搭建教程和使用教程)-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/03/image-49.png)

![[亲测可用]美团京东拼多多代付三合一版本代付源码(搭建教程+配置教程)-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/09/20240913000623356-1.webp)
![[亲测可用]易支付/码支付使用教程(梦码助手)24.07.28-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/05/image-58-1024x469.png)
![[亲测可用]写真视频打赏源码网站子比二开写真视频打赏网站源码-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/08/20240830165645811-下载.jpg)
![[亲测可搭建]模特写真网源码+数据库—zblog内核-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/05/image-97-1024x469.png)
![[亲测可用]一键关闭win10自动更新本地免安装-电脑软件-学社山中人](https://yizhi2024.top/wp-content/uploads/2025/01/20250102171808629-学社山中人_2025-01-02_17-16-51.webp)
