# Python
You can write your own custom Python codes to manipulate the received data or even process the image before passing to the next block.
def main(data):
return data
Python blocks can be cascaded.
# Installed libraries
There are a few libraries available for use.
Package name | Description |
---|---|
cv2 | OpenCV 4.2.1 |
numpy | Numpy |
torch, torchvision | PyTorch |
# Access the key-value pair
In Python, it is very simple to access the key value pair.
def main(data):
return {
"renameTheKey": data["originalKey"]
}
# Process an image
Since the image is a data URI which is comprised of a base64-encoded string, you can use a base64
package to decode the string and use cv2.imdecode
to read a numpy array.
The following sample code converts the image into a black-and-white image.
import base64
import cv2
import numpy as np
def main(data):
img = cv2.imdecode(np.asarray(bytearray(base64.b64decode(data["image"].split(",")[1])), dtype="uint8"), cv2.IMREAD_COLOR)
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert to b/w
imgstr = str(base64.b64encode(cv2.imencode(".png", img2)[1]), "utf-8")
return {
"image": "data:image/png;base64," + imgstr,
"resolution": data["resolution"]
}
# Debugging
You cannot use print()
to debug as the function is suppressed. You can always return the debugging values as the result for inspection.
← JavaScript Webhook →