Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
turkyden authored Sep 24, 2024
0 parents commit 77ecb20
Show file tree
Hide file tree
Showing 6 changed files with 437 additions and 0 deletions.
58 changes: 58 additions & 0 deletions ImageSmartCrop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from .SmartCrop import SmartCrop
from PIL import Image, ImageDraw
import torch
import numpy as np


# Tensor to PIL
def tensor2pil(image):
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))

# Convert PIL to Tensor
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)

class ImageSmartCrop:
def __init__(self):
pass

@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"stroke_width": ("INT", {
"default": 1024,
"min": 0, #Minimum value
"max": 2048, #Maximum value
"step": 1, #Slider's step
"display": "number" # Cosmetic only: display as "number" or "slider"
}),
"stroke_height": ("INT", {
"default": 1024,
"min": 0, #Minimum value
"max": 2048, #Maximum value
"step": 1, #Slider's step
"display": "number" # Cosmetic only: display as "number" or "slider"
}),
},
}

CATEGORY = "👽 ComfyLab/📐 SmartCrop 智能裁剪"
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "imageSmartCrop"

def imageSmartCrop(self, image, stroke_width, stroke_height):
cropper = SmartCrop()
image = tensor2pil(image)
result = cropper.crop(image, stroke_width, stroke_height)
box = (
result['top_crop']['x'],
result['top_crop']['y'],
result['top_crop']['width'] + result['top_crop']['x'],
result['top_crop']['height'] + result['top_crop']['y']
)
cropped_image = image.crop(box)
image_tensor = pil2tensor(cropped_image)
return (image_tensor,)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Dengju Deng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ComfyUI-SmartCrop

a ComfyUI Custom Node for [smartcrop.py](https://github.com/smartcrop/smartcrop.py)

## Usage

```bash
cd /ComfyUI/custom_nodes
```

clone from github

```bash
git clone https://github.com/Turkyden/ComfyUI-SmartCrop
```
Loading

0 comments on commit 77ecb20

Please sign in to comment.