Colorize Photo

FREEMIUM
Popularity

9.3 / 10

Latency

948ms

Service Level

99%

Health Check

100%

Back to All Tutorials (4)

Automatically colorize one image and return it in a Base64 string and print the caption (Python)

import requests
import base64

X_RapidAPI_Key = '4116843d07msh704be0dfa337826p108b3fjsn72323’
X_RapidAPI_Host = ‘colorize-photo1.p.rapidapi.com

headers = {
“X-RapidAPI-Key”: X_RapidAPI_Key,
“X-RapidAPI-Host”: X_RapidAPI_Host
}

url = “https://” + X_RapidAPI_Host + “/colorize_image_with_auto_prompt_base64"
files = {
‘image’: (‘black_and_white.jpg’, open(’ ./black_and_white.jpg’, ‘rb’), ‘image/jpeg’ )
}
data = {
“resolution”: “watermarked-sd”, # watermarked-sd, sd, full-hd, or 4k
"prompt”: “”, # replace with custom prompt
"standard_filter_id": “1”, # 1-20 default filters
"artistic_filter_id": “O”, # 1-100 more creative filters
"raw_captions": “false”, # true or false, remove post and prefix from promot
"pre_fix": “”, # create your own pre_fix, e.g. A photo of
"post fix": “”, # create your own pre_fix, e.g. HDR and colorful.
“auto_color”: “true”, # true or false, color balancing
"white_balance": “false”, # white balancing
"temperature": “-0.1”, # 0.0 - 1.0, cold or warm filter
"saturation": “1.1” # 0.0 - 2.0, adjust saturation
}

response = requests .post(url, files=files, data=data, headers=headers)

#Check if the request was successful
if response.status_code == 200:
# Get the response content in JSON format
response_json = response.json()

# Extract the image and caption values
image_base64 = response_json["image"]
caption = response_json["caption"]

# Save the image into a file
with open("colorized_image.jpg", "wb") as f:
    f.write(base64.b64decode(image_base64))

# Print the caption
print(caption)

else:
print(f"Request failed with status code: {response.status_code}")