Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
feat: add image_converter tool
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubmrx committed Feb 22, 2024
1 parent 6e5ae7b commit 6717b24
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM hyko-sdk:latest

RUN --mount=type=cache,target=/root/.cache/pip pip install opencv-python

COPY . .

CMD ["uvicorn", "--host", "0.0.0.0", "--port", "3000", "main:func"]
34 changes: 34 additions & 0 deletions hyko_toolkit/functions/utils/converters/image_converter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

import cv2
import numpy as np
from metadata import Inputs, Outputs, Params, func

from hyko_sdk.io import Image


@func.on_execute
async def main(inputs: Inputs, params: Params) -> Outputs:
"""
Convert an input image to a specified target image type.
Parameters:
- inputs (Inputs): An object containing the input image data.
- params (Params): An object containing parameters including the target image type.
Returns:
- Outputs: An object containing the converted image data.
"""
_, ext = os.path.splitext(inputs.input_image.get_name())
with open(f"./image{ext}", "wb") as f:
f.write(inputs.input_image.get_data())
image = cv2.imread(f"./image{ext}")

result_img_path = f"./image{ext.split('.')[-1]}.{params.target_type}"

success = cv2.imwrite(
result_img_path, cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
)
if success:
result = cv2.imread(result_img_path)
return Outputs(image=Image.from_ndarray(result))
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from enum import Enum

from pydantic import Field

from hyko_sdk.function import SDKFunction
from hyko_sdk.io import Image
from hyko_sdk.metadata import CoreModel


class SupportedTypes(str, Enum):
png = "png"
jpg = "jpg"
jpeg = "jpeg"
tiff = "tiff"
tif = "tif"
bmp = "bmp"
webp = "webp"
jp2 = "jp2"
dib = "dib"
pgm = "pgm"
ppm = "ppm"
pnm = "pnm"
ras = "ras"
hdr = "hdr"


func = SDKFunction(
description="Convert an input image to a specified target image type.",
)


@func.set_input
class Inputs(CoreModel):
input_image: Image = Field(..., description="Input image")


@func.set_param
class Params(CoreModel):
target_type: SupportedTypes = Field(
...,
description="The Target Type.",
)


@func.set_output
class Outputs(CoreModel):
image: Image = Field(..., description="Converted image")

0 comments on commit 6717b24

Please sign in to comment.