先上代码。 from rembg import remove import cv2 import os def list_image_files(directory): # 支持的图片文件扩展名列表 image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'] # 存储找到的图片文件路径 image_files = [] # 遍历目录 for root, dirs, files in os.walk(directory): for file in files: # 检查文件扩展名是否在支持的列表中 if any(file.lower().endswith(ext) for ext in image_extensions): # 构造完整的文件路径 full_path = os.path.join(root, file) # 将文件路径添加到列表中 image_files.append(full_path) return image_files folder_path = r'D:\picture' images = list_image_files(folder_path) i = 0 for image in images: input_path = str(image) output_path = folder_path + str(i)+'output.jpg' input = cv2.imread(input_path) output = remove(input) cv2.imwrite(output_path, output) i += 1
Python小工具—批量移除照片背景
2024-07-16 14:20:03 23 阅读