import os
import time
import requests
from pathlib import Path
X_RapidAPI_Key = '4116843d07msh704be0dfa337826p108b3fj43524’
X_RapidAPI_Host = ‘colorize-video.p.rapidapi.com’
headers = {
‘X-RapidAPI-Key’: ‘4116843d07msh704be0dfa337826p108b3fjsn7ab402961f9f’,
‘X-RapidAPI-Host’: ‘colorize-video.p.rapidapi.com’,
}
def colorize_files(input_folder, output_folder, ref_image_path):
# Note: replace jpwfm2z1g587733t with your link (see your portal)
url = “https://” + X_RapidAPI_Host + “/colorize_video_frame_with_prompt”
data = {
"resolution": "sd",
"auto_color": "false",
"white_balance": "false",
"temperature": "0.0",
"saturation": "1.0"
}
Path(output_folder).mkdir(parents=True, exist_ok=True)
for file_name in os.listdir(input_folder):
if file_name.lower().endswith(('.jpg', '.jpeg')):
input_file_path = os.path.join(input_folder, file_name)
output_file_path = os.path.join(output_folder, f'colorized_{file_name}')
for _ in range(10):
try:
files = {
'image': (file_name, open(input_file_path, 'rb'), 'image/jpeg'),
'image_ref': ('ref_image.jpg', open(ref_image_path, 'rb'), 'image/jpeg')
}
response = requests.post(url, files=files, data=data, headers=headers)
response.raise_for_status()
with open(output_file_path, 'wb') as f:
f.write(response.content)
print(f"Colorized {file_name} and saved as {output_file_path}")
break
except Exception as e:
print(f"Error while processing {file_name}: {e}")
time.sleep(60)
input_folder = './input_folder’
output_folder = './output_folder’
ref_image_path = ‘ref_image.jpg’
colorize_files(input_folder, output_folder, ref_image_path)