|
| 1 | +import requests |
| 2 | +import json |
| 3 | +import os |
| 4 | +import time |
| 5 | +from src.config import IDEOGRAM_API_KEY |
| 6 | + |
| 7 | + |
| 8 | +def ideogram_generate_cover(your_file_path): |
| 9 | + """Generater cover image using ideogram V_2 model |
| 10 | + Args: |
| 11 | + your_file_path: str, path to the image file |
| 12 | + Returns: |
| 13 | + str, local download path of the generated cover image file |
| 14 | + """ |
| 15 | + try: |
| 16 | + url = "https://api.ideogram.ai/remix" |
| 17 | + |
| 18 | + files = {"image_file": open(your_file_path, "rb")} |
| 19 | + payload = { |
| 20 | + "image_request": json.dumps( |
| 21 | + { |
| 22 | + "prompt": "This is a video screenshot, please generate a cover in the style of a manga", |
| 23 | + "aspect_ratio": "ASPECT_10_16", |
| 24 | + "image_weight": 75, |
| 25 | + "magic_prompt_option": "ON", |
| 26 | + "model": "V_2", |
| 27 | + } |
| 28 | + ) |
| 29 | + } |
| 30 | + headers = {"Api-Key": f"{IDEOGRAM_API_KEY}"} |
| 31 | + |
| 32 | + response = requests.post(url, data=payload, files=files, headers=headers) |
| 33 | + if response.status_code == 200: |
| 34 | + response_json = response.json() |
| 35 | + image_url = response_json["data"][0]["url"] |
| 36 | + img_data = requests.get(image_url).content |
| 37 | + cover_name = time.strftime("%Y%m%d%H%M%S") + ".png" |
| 38 | + temp_cover_path = os.path.join(os.path.dirname(your_file_path), cover_name) |
| 39 | + with open(temp_cover_path, "wb") as handler: |
| 40 | + handler.write(img_data) |
| 41 | + os.remove(your_file_path) |
| 42 | + return temp_cover_path |
| 43 | + else: |
| 44 | + raise Exception(response.text) |
| 45 | + except Exception as e: |
| 46 | + print(e, flush=True) |
| 47 | + return None |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + print(ideogram_generate_cover("")) |
0 commit comments