<p>I recently found out about kubeflow and kubeflow pipeline but it is not clear for me how to build an image from my python program.</p>

<p>Let's assume that I have a simple python function that crops images:</p>

<pre><code>class Image_Proc:
    def crop_image(self, image, start_pixel, end_pixel):
        # crop
        return cropped_image
</code></pre>

<p>How shall I containerize this and use it in the KubeFlow pipeline? Do I need to wrap it in an API (with Flask for example) Or do I need to connect to some media/data broker?</p>

<p>How KubeFlow pipeline sends input to this code and transfers the output of this code to the next step?</p>

<ol>
<li><p>You do not need to build images. For small to medium size components you can work on top of existing images. Check the <a href="https://github.com/kubeflow/pipelines/blob/3b97ec16b71b9287b77b1736ca2b55db281fb052/samples/core/lightweight_component/lightweight_component.ipynb" rel="nofollow noreferrer">lightweight components sample</a>.
For python see <a href="https://github.com/kubeflow/pipelines/blob/fd5778d/samples/tutorials/Data%20passing%20in%20python%20components.ipynb" rel="nofollow noreferrer">Data passing in python components</a>
For non-python see <a href="https://github.com/Ark-kun/kfp_samples/blob/ae1a5b6/2019-10%20Kubeflow%20summit/106%20-%20Creating%20components%20from%20command-line%20programs/106%20-%20Creating%20components%20from%20command-line%20programs.ipynb" rel="nofollow noreferrer">
Creating components from command-line programs</a></p></li>
<li><p>KFP SDK has some support for building container images. See the <a href="https://github.com/kubeflow/pipelines/blob/3b97ec1/samples/core/container_build/container_build.ipynb" rel="nofollow noreferrer">container_build</a> sample.</p></li>
<li><p>Read the official component authoring <a href="https://www.kubeflow.org/docs/pipelines/sdk/component-development/" rel="nofollow noreferrer">documentation</a>.</p></li>
</ol>

<blockquote>
  <p>Let's assume that I have a simple python function that crops images:</p>
</blockquote>

<p>You can just create a component from a python function like this:</p>

<pre><code>from kfp.components import InputPath, OutputPath, create_component_from_func

# Declare function (with annotations)
def crop_image(
    image_path: InputPath(),
    start_pixel: int,
    end_pixel: int,
    cropped_image_path: OutputPath(),
):
    import some_image_lib
    some_image_lib.crop(image_path, start_pixel, end_pixel, cropped_image_path)

# Create component
crop_image_op = create_component_from_func(
  crop_image,
  # base_image=..., # Optional. Base image that has most of the packages that you need. E.g. tensorflow/tensorflow:2.2.0
  packages_to_install=['some_image_lib==1.2.3'],
  output_component_file='component.yaml', # Optional. Use this to share the component between pipelines, teams or people in the world
)

# Create pipeline
def my_pipeline():
    download_image_task = download_image_op(...)

    crop_image_task = crop_image_op(
        image=download_image_task.output,
        start_pixel=10,
        end_pixel=200,
    )

# Submit pipeline
kfp.Client(host=...).create_run_from_pipeline_func(my_pipeline, arguments={})
</code></pre>
