Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unified resize node #2380

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
utility_group = image_dimensions_category.add_node_group("Utility")

resize_group.order = [
"chainner:image:resize_factor",
"chainner:image:resize_resolution",
"chainner:image:resize",
"chainner:image:resize_to_side",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from __future__ import annotations

from enum import Enum

import numpy as np

from nodes.groups import if_enum_group
from nodes.impl.pil_utils import InterpolationMethod, resize
from nodes.properties.inputs import (
EnumInput,
ImageInput,
InterpolationInput,
NumberInput,
)
from nodes.properties.outputs import ImageOutput
from nodes.utils.utils import get_h_w_c, round_half_up

from .. import resize_group


class ImageResizeMode(Enum):
PERCENTAGE = 0
ABSOLUTE = 1


@resize_group.register(
schema_id="chainner:image:resize",
name="Resize",
description=[
"Resize an image by a percent scale factor or absolute dimensions.",
"Auto uses box for downsampling and lanczos for upsampling.",
],
icon="MdOutlinePhotoSizeSelectLarge",
inputs=[
ImageInput(),
EnumInput(
ImageResizeMode, default=ImageResizeMode.PERCENTAGE, preferred_style="tabs"
).with_id(1),
if_enum_group(1, ImageResizeMode.PERCENTAGE)(
NumberInput(
"Percentage",
precision=4,
controls_step=25.0,
default=100.0,
unit="%",
hide_label=True,
).with_id(2),
),
if_enum_group(1, ImageResizeMode.ABSOLUTE)(
NumberInput("Width", minimum=1, default=1, unit="px").with_id(3),
NumberInput("Height", minimum=1, default=1, unit="px").with_id(4),
),
InterpolationInput().with_id(5),
],
outputs=[
ImageOutput(
image_type="""
let i = Input0;
let mode = Input1;

let scale = Input2;
let width = Input3;
let height = Input4;

match mode {
ImageResizeMode::Percentage => Image {
width: max(1, int & round(i.width * scale / 100)),
height: max(1, int & round(i.height * scale / 100)),
channels: i.channels,
},
ImageResizeMode::Absolute => Image {
width: width,
height: height,
channels: i.channels,
},
}
""",
assume_normalized=True,
)
],
limited_to_8bpc=True,
)
def resize_node(
img: np.ndarray,
mode: ImageResizeMode,
scale: float,
width: int,
height: int,
interpolation: InterpolationMethod,
) -> np.ndarray:
h, w, _ = get_h_w_c(img)

out_dims: tuple[int, int]
if mode == ImageResizeMode.PERCENTAGE:
out_dims = (
max(round_half_up(w * (scale / 100)), 1),
max(round_half_up(h * (scale / 100)), 1),
)
else:
out_dims = (width, height)

return resize(img, out_dims, interpolation)

This file was deleted.

This file was deleted.

73 changes: 73 additions & 0 deletions src/common/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,78 @@ const createBorderEdgesTileFillToPad: ModernMigration = (data) => {
return data;
};

const unifiedResizeNode: ModernMigration = (data) => {
interface EdgeMapping {
nodeId: string;
from: InputId | number;
to: InputId | number;
}
const edgeChanges: EdgeMapping[] = [];

const PERCENTAGE = 0;
const ABSOLUTE = 1;

data.nodes.forEach((node) => {
if (node.data.schemaId === ('chainner:image:resize_resolution' as SchemaId)) {
edgeChanges.push(
{ nodeId: node.id, from: 1, to: 3 }, // width
{ nodeId: node.id, from: 2, to: 4 }, // height
{ nodeId: node.id, from: 3, to: 5 } // interpolation
);

const oldData = node.data.inputData;
const newData: Record<number | InputId, InputValue> = {
1: ABSOLUTE,
3: oldData[1],
4: oldData[2],
5: oldData[3],
};
node.data.inputData = newData as InputData;
node.data.schemaId = 'chainner:image:resize' as SchemaId;
}

if (node.data.schemaId === ('chainner:image:resize_factor' as SchemaId)) {
edgeChanges.push(
{ nodeId: node.id, from: 1, to: 2 }, // scale
{ nodeId: node.id, from: 2, to: 5 } // interpolation
);

const oldData = node.data.inputData;
const newData: Record<number | InputId, InputValue> = {
1: PERCENTAGE,
2: oldData[1],
5: oldData[2],
};
node.data.inputData = newData as InputData;
node.data.schemaId = 'chainner:image:resize' as SchemaId;
}
});

const byNode = new Map<string, EdgeMapping[]>();
for (const mapping of edgeChanges) {
if (!byNode.has(mapping.nodeId)) {
byNode.set(mapping.nodeId, []);
}
byNode.get(mapping.nodeId)?.push(mapping);
}

data.edges.forEach((edge) => {
const mappings = byNode.get(edge.target) ?? [];
for (const mapping of mappings) {
const nodeId = mapping.nodeId;
const from = mapping.from as InputId;
const to = mapping.to as InputId;

if (edge.targetHandle === stringifyTargetHandle({ nodeId, inputId: from })) {
edge.targetHandle = stringifyTargetHandle({ nodeId, inputId: to });
break;
}
}
});

return data;
};

// ==============

const versionToMigration = (version: string) => {
Expand Down Expand Up @@ -1856,6 +1928,7 @@ const migrations = [
oldToNewIterators,
removeZIndexes,
createBorderEdgesTileFillToPad,
unifiedResizeNode,
];

export const currentMigration = migrations.length;
Expand Down