Skip to content

Commit

Permalink
Fix overlay node (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyballentine authored Mar 20, 2022
1 parent 96dce84 commit 5f24f3d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
51 changes: 36 additions & 15 deletions backend/nodes/image_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
from .properties.outputs import *


def normalize(img):
dtype_max = 1
try:
dtype_max = np.iinfo(img.dtype).max
except:
logger.debug("img dtype is not int")
return img.astype(np.float32) / dtype_max


@NodeFactory.register("Image", "Load Image")
class ImReadNode(NodeBase):
"""OpenCV Imread node"""
Expand Down Expand Up @@ -280,9 +289,19 @@ def run(
) -> np.ndarray:
"""Overlay transparent images on base image"""

base = normalize(base)
ov1 = normalize(ov1)
# overlay2 was not passed in and therefore ov2 is actually op2
if isinstance(ov2, str) or isinstance(ov2, int):
ov2 = None
op2 = None
else:
ov2 = normalize(ov2)

# Convert to 0.0-1.0 range
op1 = int(op1) / 100
op2 = int(op2) / 100
if op2 is not None:
op2 = int(op2) / 100

imgs = []
max_h, max_w, max_c = 0, 0, 1
Expand Down Expand Up @@ -318,21 +337,23 @@ def run(
center_x = imgout.shape[1] // 2
center_y = imgout.shape[0] // 2
for img, op in zip(imgs, (op1, op2)):
h, w = img.shape[:2]
if img is not None and op is not None:
h, w = img.shape[:2]

# Center overlay
x_offset = center_x - (w // 2)
y_offset = center_y - (h // 2)

cv2.addWeighted(
imgout[y_offset : y_offset + h, x_offset : x_offset + w],
1 - op,
img,
op,
0,
img,
)
imgout[y_offset : y_offset + h, x_offset : x_offset + w] = img
# Center overlay
x_offset = center_x - (w // 2)
y_offset = center_y - (h // 2)

img = cv2.addWeighted(
imgout[y_offset : y_offset + h, x_offset : x_offset + w],
1 - op,
img,
op,
0,
)
imgout[y_offset : y_offset + h, x_offset : x_offset + w] = img

imgout = np.clip(imgout, 0, 1)

return imgout

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chainner",
"productName": "chaiNNer",
"version": "0.4.0",
"version": "0.4.1",
"description": "A flowchart based image processing GUI",
"main": ".webpack/main",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion src/components/inputs/SliderInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const SliderInput = memo(({
}) => {
const { useInputData } = useContext(GlobalContext);
const [input, setInput] = useInputData(id, index);
console.log('🚀 ~ file: SliderInput.jsx ~ line 17 ~ input', input);
const [sliderValue, setSliderValue] = useState(input ?? def);
const [showTooltip, setShowTooltip] = useState(false);

Expand Down

0 comments on commit 5f24f3d

Please sign in to comment.