ComfyUI Extension: Crop To Center

Authored by leewinder

Created

Updated

0 stars

A custom node that crops images to specified dimensions from a center point with optional offset

Custom Nodes (0)

    README

    Crop To Center

    A custom node for ComfyUI that crops images to specified dimensions from a center point with optional offset, removing equal amounts from all sides.

     

    Installation

    There are two ways to install this custom node:

    ComfyUI Manager (Recommended)

    1. Install ComfyUI Manager if you don't have it yet.
    2. Go to Manager > Install Custom Nodes.
    3. Search for Crop To Center and click Install.
    4. Restart ComfyUI.

    Manual

    1. Navigate to your ComfyUI/custom_nodes/ directory.
    2. Clone the repository: git clone https://github.com/leewinder/comfyui-crop-to-center.git.
    3. Restart ComfyUI.

     

    Usage

    The Crop To Center node takes an input image and crops it to specified dimensions from the center of the image, with the option to offset the center point to move the crop region around the image.

    It removes equal amounts from all sides around the center point, while staying true to the request image dimensions.

    <img width="1024" height="1024" alt="Crop Overview" src="https://github.com/user-attachments/assets/0f928ef7-4180-4b3f-a30b-1aecc0408831" />

    The center offset can move to the edges of the image, but the crop process will prioritise the image dimensions requested, while staying as close to the center point as possible, ensuring your image is always the correct size.

    <img width="2074" height="1024" alt="Crop Edges" src="https://github.com/user-attachments/assets/079c6353-6472-495b-a5b4-721d593d623a" />

    Parameters

    <img width="298" height="190" alt="params" src="https://github.com/user-attachments/assets/fbf3091f-f5f2-477c-a1d8-28234e6696c9" />
    • image: Input image to be cropped
    • target_width: Target width for the cropped image (must be less than or equal to source width)
    • target_height: Target height for the cropped image (must be less than or equal to source height)
    • center_offset_x: Horizontal offset from the true center (positive = right, negative = left)
    • center_offset_y: Vertical offset from the true center (positive = up, negative = down)

    Outputs

    • cropped_image: The cropped image with the specified dimensions

     

    Contributing

    Contributions are welcome! Please feel free to submit a Pull Request of if you have any ideas of bugs, please raise an issue.

    Contribution Workflow

    1. Fork this repository
    2. Make your changes
    3. Test your changes
    4. Run quality checks
    5. Create a Pull Request

    Development Setup

    Prerequisites

    • python 3.9+
    • pytest 8.4+
    • pylint 3.0+

    Setting Up Your Development Environment

    1. Install pyenv:
    # macOS
    brew install pyenv
    
    # Or follow the official installation guide:
    # https://github.com/pyenv/pyenv#installation
    
    1. Clone the repository:
    git clone [email protected]:leewinder/comfyui-crop-to-center.git
    cd comfyui-crop-to-center
    
    1. Set up Python environment:
    # Create the virtual environment
    pyenv local 3.11.10
    python -m venv venv
    source venv/bin/activate
    
    1. Install dependencies:
    # Install the project and development dependencies
    pip install -e ".[dev]"
    

    Running Tests

    # Run all tests
    pytest test/test_crop.py -v
    

    Code Quality

    # Run pylint for code quality checks
    pylint src/
    pylint test/
    

     

    License

    This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International license.

     

    Contributors