首页实用工具图片批量修改(随机重命名+修改md5+镜像工具+源码)

图片批量修改(随机重命名+修改md5+镜像工具+源码)

时间2023-11-11 13:18:54分类实用工具浏览278

image.png

1、软件实现功能

对于图片文件的批量重命名、生成随机字母和数字的文件名、计算MD5值以及镜像处理

2、软件用途:

过文件内容一致性验证:有些平台再批量上传的时候会对图片进行一致性验证,是一种反作弊的方式,通过计算文件内容的 MD5 值,可以用于验证文件内容的一致性。如果文件内容没有发生变化,MD5 值也不会改变。

对每张图片进行镜像,可在一定程度上对图片进行原创处理。这在图像处理、数据增广(data augmentation)等领域中很常见。

3、实现方式:

可以使用Python脚本,结合os、hashlib、random和PIL(Python Imaging Library)模块。以下是一个示例脚本:

首先,确保安装了Pillow库,如果没有安装,可以使用以下命令安装:

pip install Pillow

然后,使用以下Python脚本进行批量处理:

import os
import hashlib
import random
import string
from PIL import Image, ImageOps
def generate_random_string(length):
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
def rename_and_mirror_images(folder_path):
image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) and f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
for image_file in image_files:
# 生成随机字母和数字的文件名
random_string = generate_random_string(10)
# 构建新文件名
new_file_name = random_string + os.path.splitext(image_file)[1]
# 构建文件路径
old_file_path = os.path.join(folder_path, image_file)
new_file_path = os.path.join(folder_path, new_file_name)
# 重命名文件
os.rename(old_file_path, new_file_path)
# 计算新文件名的MD5值
with open(new_file_path, 'rb') as f:
content = f.read()
md5_hash = hashlib.md5(content).hexdigest()
# 打开图像文件
image = Image.open(new_file_path)
# 镜像处理
mirrored_image = ImageOps.mirror(image)
# 保存镜像后的文件
mirrored_file_path = os.path.join(folder_path, f"mirrored_{new_file_name}")
mirrored_image.save(mirrored_file_path)
# 输出结果
print(f'Renamed: {new_file_name}, MD5: {md5_hash}, Mirrored: {mirrored_file_path}')
# 替换为你的图片文件夹路径
image_folder_path = 'your_image_folder_path'
# 执行批量处理
rename_and_mirror_images(image_folder_path)

确保将 your_image_folder_path 替换为你的实际图片文件夹路径。这个脚本会遍历指定文件夹中的所有图片文件,为每个文件生成一个随机的文件名,计算新文件名的MD5值,然后生成该图片的镜像,并保存到文件夹中。

2023年全球网站数量有望突破20亿大关