ComfyUI-AutoTrimBG

ComfyUI-AutoTrimBG
★ 6

自动抠图背景裁剪生成掩码图像预处理
自动清理图像背景,裁剪多余像素并保留主体,输出掩码和裁剪图像,支持可调填边,便于后续处理。
💡 快捷生成主体掩码并裁剪背景,便于合成与后期处理。
🍴 3 Forks💻 Python🔄 2025-03-10
📦
网盘下载
复制链接后前往夸克网盘下载
https://pan.quark.cn/s/79aaff81621b
📦 requirements.txt
torch==1.10.0
numpy==1.21.2
Pillow==8.4.0
Demo
📄 README

ComfyUI-AutoCropBgTrim

ComfyUI-AutoCropBgTrim is a powerful tool designed to automatically clean up the background of your images. This tool trims unnecessary spaces and pixels, leaving only the main subject of the image. It generates both a mask and an image output, making it easy to focus on the essential elements. Perfect for enhancing your photos and preparing them for professional use.

Features

  • Automatically trims backgrounds.
  • Leaves only the main subject.
  • Generates a mask and an image output.
  • Adjustable padding for cropping.
  • Installation

    To install ComfyUI-AutoCropBgTrim, clone the repository and install the dependencies:

    git clone https://github.com/yourusername/ComfyUI-AutoCropBgTrim.git
    cd ComfyUI-AutoCropBgTrim
    pip install -r requirements.txt

    Usage

    Here is an example of how to use ComfyUI-AutoCropBgTrim in your project:

    import torch
    from PIL import Image
    from ComfyUI-AutoCropBgTrim import RonLayersTrimBgUltraV2
    
    # Load your image and mask
    image = torch.load('path/to/image.pt')
    mask = torch.load('path/to/mask.pt')
    
    # Initialize the class
    cropper = RonLayersTrimBgUltraV2()
    
    # Set the padding (optional)
    padding = 10
    
    # Run the cropping function
    cropped_image, cropped_mask, crop_box, box_preview = cropper.run(image, mask, padding)
    
    # Save or process the results
    cropped_image.save('path/to/cropped_image.png')
    cropped_mask.save('path/to/cropped_mask.png')
    box_preview.save('path/to/box_preview.png')

    Class and Methods

    The main class provided by ComfyUI-AutoCropBgTrim is RonLayersTrimBgUltraV2. Below are the main methods and their descriptions:

    RonLayersTrimBgUltraV2

  • __init__: Initializes the class with default values for input and output images, masks, and cropping boxes.
  • INPUT_TYPES: Defines the input types for the class methods, including the image, mask, and padding.
  • trim_and_crop_by_mask(image, mask, padding): Trims and crops the image based on the mask, with optional padding.
  • run(image, mask, padding): Runs the trim_and_crop_by_mask method and stores the results.
  • Contributing

    If you want to contribute to ComfyUI-AutoCropBgTrim, please fork the repository and create a pull request with your changes.

    License

    ComfyUI-AutoCropBgTrim is licensed under the MIT License. See the LICENSE file for more details.

    Contact

    For any questions or feedback, please open an issue on GitHub or contact the repository owner.

    Enjoy using ComfyUI-AutoCropBgTrim for your image processing needs!