Python小工具—批量移除照片背景

先上代码。

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

相关推荐

  1. Fedora 41 Python 2支持

    2024-07-16 14:20:03       27 阅读
  2. LeetCode——1962. 石子使总数最

    2024-07-16 14:20:03       57 阅读
  3. Leetcode27题:元素【27/1000 python

    2024-07-16 14:20:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 14:20:03       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 14:20:03       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 14:20:03       62 阅读
  4. Python语言-面向对象

    2024-07-16 14:20:03       72 阅读

热门阅读

  1. 语法基础部分

    2024-07-16 14:20:03       26 阅读
  2. gradio构建webui

    2024-07-16 14:20:03       26 阅读
  3. C++中const关键字的深度探索与应用实践

    2024-07-16 14:20:03       22 阅读
  4. ChatGPT对话:如何把Html文件转换为Markdown文件

    2024-07-16 14:20:03       18 阅读
  5. 第2部分:物联网模式在行动

    2024-07-16 14:20:03       21 阅读
  6. c# 在线程中访问ui元素

    2024-07-16 14:20:03       23 阅读
  7. C语言入门-7.结构体与C++引用

    2024-07-16 14:20:03       24 阅读
  8. Python3 第二十二课 -- 装饰器

    2024-07-16 14:20:03       29 阅读