Image python snippets

Snippets to get up and running

Simple snippet to colorize one image with defaults

import requests
# 1. X_RapidAPI_Host
# 2. X_RapidAPI_Key

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"
files = {'image': ('black_and_white.jpg', open(' ./black_and_white.jpg', 'rb'), 'image/jpeg' )}

response = requests.post(url, files=files, headers=headers)
f = open( 'colorized_image.jpg', 'wb' )
f.write(response.content)
f.close()

Same snippet with the defaults explained and shown

import requests

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"
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)
f = open( 'colorized_image.jpg', 'wb' )
f.write(response.content)
f.close()

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

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}")

Use a folder of reference images to colorize one image

import os
import time
import requests
from PIL import Image

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
}

FOLDER_PATH = "test_images"  # Replace with the path to your folder
URL = "https://" + X_RapidAPI_Host + "/colorize_image_with_reference_image"


def main():
    i = 0
    for filename in os.listdir(FOLDER_PATH):
        if filename.endswith((".jpg", ".jpeg", ".png")):
            process_image(filename)
            i += 1
            time.sleep(0.5)

def process_image(filename):
    image_path = os.path.join(FOLDER_PATH, filename)
    files = {
        'image': ('black_and_white.jpeg', open('./black_and_white.jpeg', 'rb'), 'image/jpeg'),
        'image_ref': (filename, open(image_path, 'rb'), 'image/jpeg')
    }
    data = {
        "resolution": "4k",
        "auto_color": "false",
        "white_balance": "false",
        "temperature": "0.0",
        "saturation": "1.2"
    }
    response = requests.post(URL, files=files, data=data, headers=HEADERS)
    save_response_content(response, 'colorized_' + filename)

def save_response_content(response, output_filename):
    with open(output_filename, 'wb') as f:
        f.write(response.content)

if __name__ == "__main__":
    main()

Colorize several BW frames with one reference image

import os
import time
import requests
from PIL import Image

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
}

# Folder Paths
REFERENCE_IMAGE_FOLDER = "reference_image_folder"
BW_IMAGES_FOLDER = "bw_images_folder"
OUTPUT_FOLDER = "output_folder"

URL = "https://" + X_RapidAPI_Host + "/colorize_image_with_reference_image"


def main():
    i = 0
    reference_image_path = get_reference_image_path()
    
    for filename in os.listdir(BW_IMAGES_FOLDER):
        if filename.endswith((".jpg", ".jpeg", ".png")):
            process_image(filename, reference_image_path)
            i += 1
            time.sleep(0.5)

def get_reference_image_path():
    for filename in os.listdir(REFERENCE_IMAGE_FOLDER):
        if filename.endswith((".jpg", ".jpeg", ".png")):
            return os.path.join(REFERENCE_IMAGE_FOLDER, filename)
    return None

def process_image(filename, reference_image_path):
    bw_image_path = os.path.join(BW_IMAGES_FOLDER, filename)
    output_image_path = os.path.join(OUTPUT_FOLDER, 'colorized_' + filename)

    files = {
        'image': (filename, open(bw_image_path, 'rb'), 'image/jpeg'),
        'image_ref': ('reference.jpg', open(reference_image_path, 'rb'), 'image/jpeg')
    }
    
    data = {
        "resolution": "4k",
        "auto_color": "false",
        "white_balance": "false",
        "temperature": "0.0",
        "saturation": "1.2"
    }

    response = requests.post(URL, files=files, data=data, headers=HEADERS)
    save_response_content(response, output_image_path)

def save_response_content(response, output_filename):
    with open(output_filename, 'wb') as f:
        f.write(response.content)

if __name__ == "__main__":
    main()

Generate 20 Prompts

import requests
import json

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 + "/generate_image_prompt"

files = {
    'image': ('test.jpg', open('./test.jpg', 'rb'), 'image/jpeg')   
}

data = {
  "standard_filter_id": "0", # 1-20, 0 is disabled 
  "artistic_filter_id": "0", # 1-100, 0 is disabled
  "all_standard_filters": "true", # return all 20 prompts
  "all_artistic_filters": "false", # return all 100 prompts
  "raw_captions": "false" # remove pre and post fix to the caption
}

response = requests.post(url, files=files, data=data, headers=headers)
prompts = data = json.loads(response.text)

Last updated