Skip to content

Commit

Permalink
v 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
LEv145 committed Apr 4, 2023
1 parent 75db834 commit 66651c8
Show file tree
Hide file tree
Showing 13 changed files with 1,902 additions and 0 deletions.
160 changes: 160 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# XYPlot: Comfy plugin


![Image](./workflows/xy_plot_mini.png)
[Workflows](./workflows/xy_plot_mini.json)
![Image](./workflows/xy_plot_base.png)
[Workflows](./workflows/xy_plot_base.json)


## How to use

### Install

```
cd custom_nodes # From comfy path
git clone https://github.com/LEv145/XY-plot-comfy-plugin XYPlot
```
### Update

```
cd custom_nodes/XYPlot
git pull
```
8 changes: 8 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .src import ImageSetAreaNode, FloatImageCombineNode, XYPlotNode


NODE_CLASS_MAPPINGS = {
"ImageSetArea": ImageSetAreaNode,
"FloatImageCombine": FloatImageCombineNode,
"XYPlot": XYPlotNode,
}
3 changes: 3 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .nodes.image_set_area import ImageSetAreaNode
from .nodes.float_image_combine import FloatImageCombineNode
from .nodes.xy_plot import XYPlotNode
10 changes: 10 additions & 0 deletions src/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typing as t


class BasePlotNode():
CATEGORY: str = "XYPlot"
FUNCTION: str = "execute"


Image = t.Any
FloatImage = list[Image]
19 changes: 19 additions & 0 deletions src/nodes/float_image_combine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import typing as t

from ..base import BasePlotNode, FloatImage


class FloatImageCombineNode(BasePlotNode):
RETURN_TYPES: t.Tuple[str] = ("FLOAT_IMAGE",)

@classmethod
def INPUT_TYPES(cls) -> t.Dict[str, t.Any]:
return {
"required": {
"float_image_1": ("FLOAT_IMAGE",),
"float_image_2": ("FLOAT_IMAGE",),
},
}

def execute(self, float_image_1: FloatImage, float_image_2: FloatImage) -> tuple[FloatImage]:
return (float_image_1 + float_image_2,)
21 changes: 21 additions & 0 deletions src/nodes/image_set_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import typing as t

from ..base import BasePlotNode, FloatImage, Image


class ImageSetAreaNode(BasePlotNode):
RETURN_TYPES: t.Tuple[str] = ("FLOAT_IMAGE",)

def __init__(self):
pass

@classmethod
def INPUT_TYPES(cls) -> t.Dict[str, t.Any]:
return {
"required": {
"image": ("IMAGE",),
},
}

def execute(self, image: Image) -> tuple[FloatImage]:
return ([image],)
30 changes: 30 additions & 0 deletions src/nodes/xy_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import typing as t

from ..base import BasePlotNode, FloatImage, Image
from ..utils import tensor_to_pillow, pillow_to_tensor, create_image_grid


class XYPlotNode(BasePlotNode):
RETURN_TYPES: t.Tuple[str] = ("IMAGE",)

@classmethod
def INPUT_TYPES(cls) -> t.Dict[str, t.Any]:
return {
"required": {
"float_image": ("FLOAT_IMAGE",),
"gap": ("INT", {"default": 0, "min": 0}),
"nrow": ("INT", {"default": 1, "min": 1}),
},
}

def execute(
self,
float_image: FloatImage,
nrow: int,
gap: int
) -> tuple[Image]:
pillow_images = [tensor_to_pillow(i) for i in float_image]
pillow_grid = create_image_grid(pillow_images, nrow=nrow, gap=gap)
tensor_grid = pillow_to_tensor(pillow_grid)

return (tensor_grid,)
47 changes: 47 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import typing as t

import torch
import numpy as np
from PIL import Image


def tensor_to_pillow(image):
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))


def pillow_to_tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)


def create_image_grid(images, gap, nrow):
"""
Create a grid of images with a specified gap and number of rows.
Args:
images (List[PIL.Image.Image]): List of images to be placed in the grid.
gap (int, optional): Gap between images in pixels. Defaults to 10.
nrow (int, optional): Number of rows in the grid. Defaults to 3.
Returns:
PIL.Image.Image: The merged image grid.
"""
# Calculate number of columns based on number of rows and images
ncol = (len(images) + nrow - 1) // nrow

# Get size of each image in pixels
image_width, image_height = images[0].size

# Create new image to hold the grid
grid_width = ncol * image_width + (ncol - 1) * gap
grid_height = nrow * image_height + (nrow - 1) * gap
grid_image = Image.new("RGB", (grid_width, grid_height), color="white")

# Paste images into grid
for i, image in enumerate(images):
row = i // ncol
col = i % ncol
x = col * (image_width + gap)
y = row * (image_height + gap)
grid_image.paste(image, (x, y))

return grid_image
Loading

0 comments on commit 66651c8

Please sign in to comment.