How to Create A Component
It is recommended to create a component using templates instead of starting from scratch. Go to browser and click on Create new component
, select python Crop Image
template. This will generate an example component for cropping an image. It is recommended to modify it rather than create everything from scratch.
It is possible to use PPL Tool for initializing and creating more complicated components.
Steps
Component Structure
This is should be your folder structure after you create component with template
Structure for Dot Product template
.
├── component.yml
├── .pipecomponent
├── src
│ ├── requirements.txt
│ └── main.py
└── tests
├── test_0
│ ├── input_0.txt
│ ├── input_1.txt
│ └── output_0.txt
└── test_1
├── input_0.txt
├── input_1.txt
└── output_0.txt
Structure for Crop Image template
.
├── component.yml
├── .pipecomponent
├── src
├── requirements.txt
└── main.py
Configuration File
name: "Crop Image Python"
language: py
platform: linux
build_system: 1.1.3
worker:
input_type: "Image"
output_type: "Image"
config_schema:
top_left:
type: (UInt64, UInt64)
bottom_right:
type: (UInt64, UInt64)
Crop Image Source
from pipelogic.worker import run, config
import pipelogic
import numpy as np
def crop_image(Image):
raw_data = np.reshape(np.array(Image.data, dtype='uint8'), (Image.height, Image.width, 3))
x1 = config.top_left[0]
y1 = config.top_left[1]
x2 = config.bottom_right[0]
y2 = config.bottom_right[1]
image_cropped = raw_data[x1:x2, y1:y2]
return { "height": image_cropped.shape[0],
"width": image_cropped.shape[1],
"data": image_cropped.flatten().tobytes(),
"format": Image.format}
run(crop_image)
Rewrite the component
Convert Image to Gray
will process images and will output images. Therefore, the input and output types stay same. We need to remove config_schema
section since it does not require any parameter. We also nned to change the name of the component.
Configuration
name: "Convert Image to Gray"
language: py
platform: linux
build_system: 1.1.3
tags: ["one-to-one", "image-manipulation"]
worker:
input_type: "Image"
output_type: "Image"
Dependencies
Inside requirements.txt
, first library provided pipelogic api and the other ones are needed for image manipulation.
pipelogic
numpy
opencv-python-headless # headless version is for servers
Component function
Add libraries to source code and change the source code by replacing crop_image
with convert_gray
.
from pipelogic.worker import run, config
import pipelogic
import numpy as np
import cppipe
import cv2
Image_GRAY = cppipe.create_named('Image.GRAY', cppipe.create_tuple([]))
def convert_gray(Image):
image = np.reshape(np.array(Image.data, dtype='uint8'), (Image.height, Image.width, 3))
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return {
"height": Image.height,
"width": Image.width,
"data": gray_image.flatten().tobytes(),
"format": pipelogic.types.Named(pipelogic.types.Tuple(()), Image_GRAY)
}
run(convert_gray)
Extend Color Conversion
Add additional format definitions and a function to make component work for every channel format.
from pipelogic.worker import run, config
import pipelogic
import numpy as np
import cppipe
import cv2
Image_BGR = cppipe.create_named('Image.BGR', cppipe.create_tuple([]))
Image_RGB = cppipe.create_named('Image.RGB', cppipe.create_tuple([]))
Image_GRAY = cppipe.create_named('Image.GRAY', cppipe.create_tuple([]))
Image_RGBA = cppipe.create_named('Image.RGBA', cppipe.create_tuple([]))
Image_BGRA = cppipe.create_named('Image.BGRA', cppipe.create_tuple([]))
# converts all possible image formats into gray image
def gray_conversion(Image):
image_data = np.array(Image.data, dtype='uint8')
if Image.format == Image_BGR:
image = np.reshape(image_data, (Image.height, Image.width, 3))
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
elif Image.format == Image_RGB:
image = np.reshape(image_data, (Image.height, Image.width, 3))
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
elif Image.format == Image_RGBA:
image = np.reshape(image_data, (Image.height, Image.width, 4))
return cv2.cvtColor(image, cv2.COLOR_RGBA2GRAY)
elif Image.format == Image_BGRA:
image = np.reshape(image_data, (Image.height, Image.width, 4))
return cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
else:
image = np.reshape(image_data, (Image.height, Image.width, 1))
return image
def convert_gray(Image):
gray_image = gray_conversion(Image)
return {
"height": Image.height,
"width": Image.width,
"data": gray_image.flatten().tobytes(),
"format": pipelogic.types.Named(pipelogic.types.Tuple(()), Image_GRAY)
}
run(convert_gray)