This document describes how to integrate Karlo APIs into your service with a REST API.
As of February 22, 2024, the Karlo model used in the Karlo API has been upgraded to version 2.1 with enhanced image generation quality. Previous version information can be found in the legacy document.
Each API of Karlo has different applicable model versions. You can select the model version to apply with version
. We recommend using version 2.1, which has enhanced image generation quality. Version 2.1 can generate images up to 1280 pixels in both width and height, while version 2.0 can generate images up to 640 pixels in size.
Refer to the list of applicable model versions for each API below.
v2.1
, v2.0
v2.1
, v2.0
v2.0
The original image must comply with the specifications below.
JPG
, PNG
, webp
(Recommended)Karlo API handles the image file for the request and the response as a Base64 encoded string value.
image
parameter.image
field and save it as a PNG
image file.Below is a sample for Base64 encoding and decoding.
import io
import base64
from PIL import Image
# Base64 encoding
def imageToString(img):
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
my_encoded_img = base64.encodebytes(img_byte_arr.getvalue()).decode('ascii')
return my_encoded_img
# Base64 decoding and saving as a PNG file
def stringToImage(base64_string, mode='RGBA'):
imgdata = base64.b64decode(str(base64_string))
img = Image.open(io.BytesIO(imgdata)).convert(mode)
return img
# Image loading
img = Image.open('my_image.png')
# Base64 encoding
img_base64 = imageToString(img)
# Decoding and saving
image = stringToImage(img_base64, mode='RGB')
Method | URL | Authorization |
---|---|---|
POST |
https://api.kakaobrain.com/v2/inference/karlo/t2i |
REST API Key |
Permission | Prerequisite | Kakao Login | User consent |
---|---|---|---|
- | - | - | - |
Create new images based on the given text prompt.
Include the REST API key in the request header, and send a POST
request. You must specify the prompt in the request body. Refer to How to use to request more efficiently with optional parameters.
If failed, refer to Error code to figure out the reason.
Name | Description | Required |
---|---|---|
Authorization | Authorization: KakaoAK ${REST_API_KEY} REST API key as a type of user authentication. |
O |
Content-Type | Content-Type: application/json The data type of the request. |
O |
Name | Type | Description | Required |
---|---|---|---|
version | String |
Karlo model version to be applied to the request. One of:v2.1 : Version 2.1v2.0 : Version 2.0 |
O |
prompt | String |
A text describing the desired image. (Maximum: 2048 characters) |
O |
negative_prompt | String |
A text describing anything you want to exclude or control from the image when it is generated. (Maximum: 2048 characters) |
X |
width | Integer |
Width of the image (Unit: pixel) Values vary depending on the applied model version ( version ), refer to the below.v2.1 : A multiple of 8 (Recommended: 1024, Minumum: 768, Maximum: 1280)v2.0 : A multiple of 8 (Recommended: 512, Minumum: 384, Maximum: 640) |
O |
height | Integer |
Height of the image (Unit: pixel) Values vary depending on the applied model version ( version ), refer to the below.v2.1 : A multiple of 8 (Recommended: 1024, Minumum: 768, Maximum: 1280)v2.0 : A multiple of 8 (Recommended: 512, Minumum: 384, Maximum: 640) |
O |
upscale | Boolean |
Whether to upscale the generated image.true : Upscalefalse : Do not upscale |
X |
scale | Integer |
Rate to upscale the image, 2 or 4. (Default: 2) Note: Even if the scale is set to 4, you can only upscale the image up to 2048 pixels in width and height. |
X |
image_format | String |
Format to save the image in. One of:webp jpeg png (Default: webp ) |
X |
image_quality | Integer |
Save Image Quality. (Default: 70, Minumum: 1, Maximum: 100) |
X |
samples | Integer |
Number of images to generate. (Default: 1, Minumum: 1, Maximum: 8) |
X |
return_type | String |
Type of the image to return. One of:base64_string : A Base64 encoded value of the image fileurl : A URL of the image file(Default: url ) |
X |
prior_num_inference_steps | Integer |
The number of steps of the prior denoising process. (Default: 25, Minumum: 10, Maximum: 100) |
X |
prior_guidance_scale | Double |
Guidance scale of the prior denoising process. (Default: 5.0, Minumum: 1.0, Maximum: 20.0) |
X |
num_inference_steps | Integer |
The number of steps of the decoder denoising process. (Default: 50, Minumum: 10, Maximum: 100) |
X |
guidance_scale | Double |
Guidance scale of denoising process. (Default: 5.0, Minumum: 1.0, Maximum: 20.0) |
X |
scheduler | String |
Scheduler used by decoder denoising process Values vary depending on the applied model version ( version ), refer to the below.v2.1 : decoder_ddim_v_prediction (Default)v2.0 : One of decoder_ddim_v_prediction (Default) or decoder_ddpm_v_prediction |
X |
seed | Integer[] |
A seed of length equal to the batch size length.seed must have the same lengh with the number of samples .Composed of numbers greater than or equal to 0 and less than or equal to 4,294,967,295. If none, a random. (Default: null )Note: Karlo always generates the same image, if all parameters, including seed , have the same value. |
X |
nsfw_checker | Boolean |
Whether to perform Detect NSFW content on the generated image.true : Performfalse : Do not perform(Default: false ) |
X |
face_refiner | FaceRefiner |
Setting parameters for refine facial structure. Note: Recommended for use only in the model version ( version ). |
X |
Name | Type | Description | Required |
---|---|---|---|
bbox_size_threshold | Double |
Ratio of facial area to total image to apply face shape refinement feature, apply feature only if actual facial area is smaller than set size (Default: 1, Minumum: 0, Maximum: 1) |
X |
bbox_filter_threshold | Double |
Threshold to determine if the image is a human face, not applied if 0 .(Default: 1, Minumum: 0, Maximum: 1) |
X |
restoration_repeats | Double |
Number of facial structure refinements applied (Default: 2, Minumum: 1, Maximum: 10) |
X |
weight_sft | Double |
Original image retention weight determines how much of the original image is retained. The higher the value, the more of the original image is preserved. (Default: 0.5, Minumum: 0, Maximum 1) |
X |
Name | Type | Description | Required |
---|---|---|---|
id | String |
Transaction ID | O |
model_version | String |
The version of Karlo model that processed the request. | O |
images | Image[] |
An array of generated images. | O |
Name | Type | Description |
---|---|---|
id | String |
Image ID |
seed | Integer |
The seed value used to generate this image. Used to regenerate an image with the same content. |
image | String |
Image file. A Base64 string or URL depends on the return_type value of the request.URL is only active for 10 minutes from the response time. |
nsfw_content_detected | Boolean |
Whether the image includes NSFW(Not Safe For Work) content.true : Includedfalse : Not includedNote: Return null when unused. |
nsfw_score | Double |
NSFW score of the image. Note: Return null when unused. |
# Libraries for REST API requests and image processing
import requests
import json
import urllib
from PIL import Image
# Input the REST API key in [My Application] > [App keys]
REST_API_KEY = '${REST_API_KEY}'
# Generate image
def t2i(prompt, negative_prompt):
r = requests.post(
'https://api.kakaobrain.com/v2/inference/karlo/t2i',
json = {
"version": "v2.1",
"prompt": prompt,
"negative_prompt": negative_prompt,
"height": 1024,
"width": 1024
},
headers = {
'Authorization': f'KakaoAK {REST_API_KEY}',
'Content-Type': 'application/json'
}
)
# Convert the response to JSON
response = json.loads(r.content)
return response
# Prompt
prompt = "A photo of a cute tiny monster on the beach, daylight."
negative_prompt = ""
# Call the API
response = t2i(prompt, negative_prompt)
# Show the first image of the response
result = Image.open(urllib.request.urlopen(response.get("images")[0].get("image")))
result.show()
curl -v -X POST "https://api.kakaobrain.com/v2/inference/karlo/t2i" \
-H "Authorization: KakaoAK ${REST_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"version": "v2.1",
"prompt": "A photo of a cute tiny monster on the beach, daylight.",
"height": 1024,
"width": 1024
}'
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "3d6fb820-9845-4b3e-8d6d-7cfd01db5649",
"model_version": "${MODEL_VERSION}",
"images": [
{
"id": "a80108f8-b9c6-4bf4-aa38-957a38ced4a8",
"seed": 3878985944,
"image": "https://mk.kakaocdn.net/dna/karlo/image/..."
}
]
}
Method | URL | Authorization |
---|---|---|
POST |
https://api.kakaobrain.com/v2/inference/karlo/variations |
REST API Key |
Permission | Prerequisite | Kakao Login | User consent |
---|---|---|---|
- | - | - | - |
Create variations of the given image.
Include the REST API key in the request header, and send a POST
request. Refer to How to use to request more efficiently with optional parameters.
If failed, refer to Error code to figure out the reason.
Name | Description | Required |
---|---|---|
Authorization | Authorization: KakaoAK ${REST_API_KEY} REST API key as a type of user authentication. |
O |
Content-Type | Content-Type: application/json The data type of the request. |
O |
Name | Type | Description | Required |
---|---|---|---|
version | String |
Karlo model version to be applied to the request. One of:v2.1 : Version 2.1v2.0 : Version 2.0 |
O |
image | String |
A Base64 encoded value of the original image file. | O |
width | Integer |
Width of the image (Unit: pixel) Values vary depending on the applied model version ( version ), refer to the below.v2.1 : A multiple of 8 (Recommended: 1024, Minumum: 768, Maximum: 1280)v2.0 : A multiple of 8 (Recommended: 512, Minumum: 384, Maximum: 640) |
O |
height | Integer |
Height of the image (Unit: pixel) Values vary depending on the applied model version ( version ), refer to the below.v2.1 : A multiple of 8 (Recommended: 1024, Minumum: 768, Maximum: 1280)v2.0 : A multiple of 8 (Recommended: 512, Minumum: 384, Maximum: 640) |
O |
upscale | Boolean |
Whether to upscale the generated image.true : Upscalefalse : Do not upscale |
X |
scale | Integer |
Rate to upscale the image, 2 or 4. (Default: 2) Note: Even if the scale is set to 4, you can only upscale the image up to 2048 pixels in width and height. |
X |
image_format | String |
Format to save the image in. One of:webp jpeg png (Default: webp ) |
X |
image_quality | Integer |
Save Image Quality. (Default: 70, Minumum: 1, Maximum: 100) |
X |
samples | Integer |
Number of images to generate. (Default: 1, Minumum: 1, Maximum: 8) |
X |
return_type | String |
Type of the image to return. One of:base64_string : A Base64 encoded value of the image fileurl : A URL of the image file(Default: url ) |
X |
num_inference_steps | Integer |
The number of steps of the decoder denoising process. (Default: 50, Minumum: 10, Maximum: 100) |
X |
guidance_scale | Double |
Guidance scale of denoising process. (Default: 5.0, Minumum: 1.0, Maximum: 20.0) |
X |
scheduler | String |
Scheduler used by decoder denoising process Values vary depending on the applied model version ( version ), refer to the below.v2.1 : decoder_ddim_v_prediction (Default)v2.0 : One of decoder_ddim_v_prediction (Default) or decoder_ddpm_v_prediction |
X |
seed | Integer[] |
A seed of length equal to the batch size length.seed must have the same lengh with the number of samples .Composed of numbers greater than or equal to 0 and less than or equal to 4,294,967,295. If none, a random. (Default: null )Note: Karlo always generates the same image, if all parameters, including seed , have the same value. |
X |
nsfw_checker | Boolean |
Whether to perform Detect NSFW content on the generated image.true : Performfalse : Do not perform(Default: false ) |
X |
face_refiner | FaceRefiner |
Setting parameters for refine facial structure. Note: Recommended for use only in the model version ( version ). |
X |
Name | Type | Description |
---|---|---|
id | String |
Transaction ID |
model_version | String |
The version of Karlo model that processed the request. |
images | Image[] |
An array of generated images. |
# Libraries for REST API requests and image processing
import requests
import json
import io
import base64
import urllib
from PIL import Image
# Input the REST API key in [My Application] > [App keys]
REST_API_KEY = '${REST_API_KEY}'
# Make variation
def variations(image):
r = requests.post(
'https://api.kakaobrain.com/v2/inference/karlo/variations',
json = {
'version': 'v2.1',
'image': image,
'height': 1024,
'width': 1024
},
headers = {
'Authorization': f'KakaoAK {REST_API_KEY}',
'Content-Type': 'application/json'
}
)
# Convert the response to JSON
response = json.loads(r.content)
return response
# Base64 encoding
def imageToString(img):
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
my_encoded_img = base64.encodebytes(img_byte_arr.getvalue()).decode('ascii')
return my_encoded_img
# Loading image
img = Image.open('original.png')
# Base64 encoding
img_base64 = imageToString(img)
# Call the API
response = variations(img_base64)
print(response)
# Show the first image of the response
result = Image.open(urllib.request.urlopen(response.get("images")[0].get("image")))
result.show()
curl -v -X POST "https://api.kakaobrain.com/v2/inference/karlo/variations" \
-H "Authorization: KakaoAK ${REST_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"version": "v2.1",
"image": "iVBORw0KGgoAAAANSUhEUgA...",
"height": 1024,
"width": 1024
}'
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "6c0db22f-ecba-4d84-a817-b06e06a207da",
"model_version": "${MODEL_VERSION}",
"images": [
{
"id": "812702f0-3627-4a32-91cc-eaec50910156",
"image": "https://mk.kakaocdn.net/dna/karlo/image/...",
"seed": 2639508875
}
]
}
Method | URL | Authorization |
---|---|---|
POST |
https://api.kakaobrain.com/v2/inference/karlo/inpainting |
REST API key |
Permission | Prerequisite | Kakao Login | User consent |
---|---|---|---|
- | - | - | - |
Creates a connected image based on the given image.
Include the REST API key in the request header, and send a POST
request. Refer to How to use to request more efficiently with optional parameters.
If failed, refer to Error code to figure out the reason.
Name | Description | Required |
---|---|---|
Authorization | Authorization: KakaoAK ${REST_API_KEY} REST API key as a type of user authentication. |
O |
Content-Type | Content-Type: application/json The data type of the request. |
O |
Name | Type | Description | Required |
---|---|---|---|
version | String |
Karlo model version to be applied to the request, applicable version 2.0 only. (Default: v2.0 ) |
X |
prompt | String |
A text describing the desired image. (Maximum: 2048 characters) |
O |
negative_prompt | String |
A text describing anything you want to exclude or control from the image when it is generated. (Maximum: 2048 characters) |
X |
image | String |
A Base64 encoded value of the original image file, refer to Image file specification Images must be with a size of up to 1024 pixels in width and height. Supports webp , png , jpeg , heic file formats. |
O |
mask | String |
A Base64 encoded value of the image with the area to be edited colored black (RGB or Grayscale value 0 ), refer to Image file specification |
O |
reference_image | String |
A Base64 encoded value of the image to reference when calibrating the edit area Images must be with a size of up to 1024 pixels in width and height. Supports webp , png , jpeg , heic file formats. |
X |
upscale | Boolean |
Whether to upscale the generated image.true : Upscalefalse : Do not upscale |
X |
scale | Integer |
Rate to upscale the image, 2 or 4. (Default: 2) Note: Even if the scale is set to 4, you can only upscale the image up to 2048 pixels in width and height. |
X |
image_format | String |
Format to save the image in. One of:webp jpeg png (Default: webp ) |
X |
image_quality | Integer |
Save Image Quality. (Default: 70, Minumum: 1, Maximum: 100) |
X |
samples | Integer |
Number of images to generate. (Default: 1, Minumum: 1, Maximum: 8) |
X |
return_type | String |
Type of the image to return. One of:base64_string : A Base64 encoded value of the image fileurl : A URL of the image file(Default: url ) |
X |
prior_num_inference_steps | Integer |
The number of steps of the prior denoising process. (Default: 25, Minumum: 10, Maximum: 100) |
X |
prior_guidance_scale | Double |
Guidance scale of the prior denoising process. (Default: 5.0, Minumum: 1.0, Maximum: 20.0) |
X |
num_inference_steps | Integer |
The number of steps of the decoder denoising process. (Default: 50, Minumum: 10, Maximum: 100) |
X |
guidance_scale | Double |
Guidance scale of denoising process. (Default: 5.0, Minumum: 1.0, Maximum: 20.0) |
X |
seed | Integer[] |
A seed of length equal to the batch size length.seed must have the same lengh with the number of samples .Composed of numbers greater than or equal to 0 and less than or equal to 4,294,967,295. If none, a random. (Default: null )Note: Karlo always generates the same image, if all parameters, including seed , have the same value. |
X |
nsfw_checker | Boolean |
Whether to perform Detect NSFW content on the generated image.true : Performfalse : Do not perform(Default: false ) |
X |
face_refiner | FaceRefiner |
Setting parameters for refine facial structure. | X |
Name | Type | Description |
---|---|---|
id | String |
Transaction ID |
model_version | String |
The version of Karlo model that processed the request. |
images | Image[] |
An array of generated images. |
# Libraries for REST API requests and image processing
import requests
import json
import io
import base64
import urllib
from PIL import Image
# Input the REST API key in [My Application] > [App keys]
REST_API_KEY = '${REST_API_KEY}'
# Modify image
def inpainting(image, mask):
r = requests.post(
'https://api.kakaobrain.com/v2/inference/karlo/inpainting',
json = {
'image': image,
'mask': mask
},
headers = {
'Authorization': f'KakaoAK {REST_API_KEY}',
'Content-Type': 'application/json'
}
)
# Convert the response to JSON
response = json.loads(r.content)
return response
# Base64 encoding
def imageToString(img):
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
my_encoded_img = base64.encodebytes(img_byte_arr.getvalue()).decode('ascii')
return my_encoded_img
# Loading image
img = Image.open('image.png')
mask = Image.open('mask.png')
# Base64 encoding
img_base64 = imageToString(img)
mask_base64 = imageToString(mask)
# Call the API
response = inpainting(img_base64,mask_base64)
print(response)
# Show the first image of the response
result = Image.open(urllib.request.urlopen(response.get("images")[0].get("image")))
result.show()
curl -v -X POST "https://api.kakaobrain.com/v2/inference/karlo/variations" \
-H "Authorization: KakaoAK ${REST_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"prompt": "tiger",
"image": "https://mk.kakaocdn.net/dn/karlo-dev/...",
"mask": "https://mk.kakaocdn.net/dn/karlo-dev/...",
"negative_prompt": "dog, human",
"samples": 1,
"prior_num_inference_steps": 25,
"prior_guidance_scale": 5,
"num_inference_steps": 50,
"guidance_scale": 5,
"seed": [0],
"nsfw_checker": true,
"upscale": false,
"scale": 2,
"face_refiner": {
"bbox_size_threshold": 1,
"bbox_filter_threshold": 0.5,
"restoration_repeats": 2,
"weight_sft": 0.5
},
"image_format": "webp",
"image_quality": 70
}'
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "3d6fb820-9845-4b3e-8d6d-7cfd01db5649",
"model_version": "${MODEL_VERSION}",
"images": [
{
"id": "a80108f8-b9c6-4bf4-aa38-957a38ced4a8",
"seed": 3878985944,
"image": "https://mk.kakaocdn.net/dna/karlo/image/..."
}
]
}