一个处理python图片的程序,重命名, 删除,copy等
import os
import shutil
def write_filenames_to_txt(directory, txt_path):
"""
获取传入文件夹名内的所有文件名,并写入txt
"""
with open(txt_path, 'w') as file:
for filename in os.listdir(directory):
file.write(filename + '\n')
print("wirte file list in image_files2.txt success")
def delete_files_from_txt(directory, txt_path):
"""
读取传入txt文件的图片名称,并在传入文件夹中删除
"""
with open(txt_path, 'r') as file:
filenames = file.read().splitlines()
for filename in filenames:
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path):
os.remove(file_path)
print("delete success")
def copy_files_from_txt_to_directory(txt_path, src_directory, dest_directory):
"""
复制传入txt文件中列出的图片到指定文件夹
"""
with open(txt_path, 'r') as file:
filenames = file.read().splitlines()
if not os.path.exists(dest_directory):
os.makedirs(dest_directory)
for filename in filenames:
src_path = os.path.join(src_directory, filename)
if os.path.isfile(src_path):
shutil.copy(src_path, dest_directory)
def renamejpg(folder_path):
"""
传入参数为文件夹名,将相同名字的jpg文件和cr2文件同时按照数字递增重命名
"""
prefix = "A" # 文件名前缀
start_index = 1 # 起始索引
# 获取文件夹中的所有文件
file_list = os.listdir(folder_path)
# 遍历文件列表
for index, file_name in enumerate(file_list):
if file_name.endswith(".jpg") or file_name.endswith(".png"):
# 构建新的文件名
new_file_name = f"{prefix}{str(start_index + index).zfill(3)}" + os.path.splitext(file_name)[1]
# 构建文件的完整路径
old_file_path = os.path.join(folder_path, file_name)
new_file_path = os.path.join(folder_path, new_file_name)
# 重命名文件
os.rename(old_file_path, new_file_path)
print("rename jpg or png success!")
def renamejpgandcr2(folder_path):
"""
传入参数为文件夹名,将相同名字的jpg文件和cr2文件同时按照数字递增重命名
"""
prefix = "B" # 文件名前缀
start_index = 1 # 起始索引
# 获取文件夹中的所有jpg和cr2文件
jpg_files = sorted([f for f in os.listdir(folder_path) if f.lower().endswith('.png')])
cr2_files = sorted([f for f in os.listdir(folder_path) if f.lower().endswith('.cr2')])
# 确保文件名列表按照相同基名排序
jpg_files.sort()
cr2_files.sort()
for index, jpg_file in enumerate(jpg_files):
base_name = os.path.splitext(jpg_file)[0]
cr2_file = base_name + '.cr2'
if cr2_file in cr2_files:
new_file_name = f"{prefix}{str(start_index + index).zfill(3)}"
# 构建文件的完整路径
old_jpg_path = os.path.join(folder_path, jpg_file)
new_jpg_path = os.path.join(folder_path, new_file_name + '.png')
old_cr2_path = os.path.join(folder_path, cr2_file)
new_cr2_path = os.path.join(folder_path, new_file_name + '.cr2')
# 重命名文件
os.rename(old_jpg_path, new_jpg_path)
os.rename(old_cr2_path, new_cr2_path)
print("rename jpg and cr2 success!")
def copy_jpg_to_cr2_in_txt(txt_path):
"""
读取 txt 文件,将其中所有 .jpg 图片名称复制一份并重写成 .cr2
"""
with open(txt_path, 'r') as file:
lines = file.readlines()
new_lines = []
for line in lines:
line = line.strip()
new_lines.append(line)
if line.lower().endswith('.png'):
new_line = line[:-4] + '.cr2'
new_lines.append(new_line)
with open(txt_path, 'w') as file:
for line in new_lines:
file.write(line + '\n')
print(f"All .jpg filenames in {txt_path} have been copied as .cr2")
def main():
while True:
print("选择一个操作:")
print("1. 重命名 jpg 和 cr2 文件")
print("2. 将文件夹中的所有文件名写入 txt 文件") # 2几乎不咋用吧,作用是获取想删除的文件,并delete
print("3. 从 txt 文件中读取文件名并删除这些文件")
print("4. 从 txt 文件中读取文件名并复制这些文件到指定文件夹")
print("5. 重命名所有jpg或者png文件")
print("6. 将txt里的文件写入一份cr2类型的")
print("0. 退出程序")
#folder_path = input("输入文件夹路径: ")
folder_path=".\d"
copy_txt_path="copy.txt"
output_file = "image_files2.txt"
delete_txt_path="delete.txt"
choice = input("输入操作编号 (1-6): ")
dest_directory=".\de"
# 创建目标文件夹
os.makedirs(dest_directory, exist_ok=True)
if choice == '1':
renamejpgandcr2(folder_path)
elif choice == '2':
write_filenames_to_txt(folder_path, output_file)
elif choice == '3':
delete_files_from_txt(folder_path, delete_txt_path)
elif choice == '4':
copy_files_from_txt_to_directory(output_file, folder_path, dest_directory)
elif choice == '5':
renamejpg(folder_path)
elif choice == '6':
copy_jpg_to_cr2_in_txt(output_file)
elif choice == '0':
print("退出程序。")
break
else:
print("无效的选择,请输入 1 到 4 之间的数字。")
if __name__ == "__main__":
main()
# 示例调用方法
#renamejpgandcr2_files('/path/to/your/directory')
#2获取文件夹里面的所有文件名list
#output_file = "image_files2.txt"
#write_filenames_to_txt('./p', output_file)
# delete_files_from_txt('/path/to/your/directory', '/path/to/your/file.txt')
# copy_files_from_txt_to_directory('/path/to/your/file.txt', '/path/to/your/src_directory', '/path/to/your/dest_directory')
#5 重命名文件里面的png文件,jpg文件同理
#renamejpg('./p')
#6
#renamejpgandcr2('./p')