Last active
March 8, 2026 06:25
-
-
Save kadnan/77993c218999544ee7a09e6232c5dc90 to your computer and use it in GitHub Desktop.
AI Product Image generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "id": "c2feecd4-2a16-4960-a2f1-1eac8b55e6b4", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import openai\n", | |
| "import base64\n", | |
| "import os\n", | |
| "from pathlib import Path\n", | |
| "from dotenv import load_dotenv" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "id": "d1d6f7c8-5a56-4991-9653-7c15b9836d16", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "load_dotenv()\n", | |
| "API_KEY = os.environ.get(\"API_KEY\")\n", | |
| "client = openai.OpenAI(api_key=API_KEY)\n", | |
| "\n", | |
| "def encode_image(image_path):\n", | |
| " with open(image_path, \"rb\") as f:\n", | |
| " return base64.b64encode(f.read()).decode(\"utf-8\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "id": "3370eb29-18b9-4798-85da-f37461da7870", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "prompt = \"\"\"Place this product in a natural lifestyle setting. Warm golden-hour lighting, \n", | |
| "shallow depth of field, editorial style. Product details unchanged. \n", | |
| "Instagram-worthy composition.\"\"\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "id": "1a52e8eb-dcd9-417c-994b-1dde6584ddc7", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "β Saved to product_studio.png\n", | |
| "π Token Usage:\n", | |
| " Input tokens: 388\n", | |
| " Output tokens: 4534\n", | |
| " Total tokens: 4922\n", | |
| "\n", | |
| "π° Estimated Cost:\n", | |
| " Input: $0.003104\n", | |
| " Output: $0.145088\n", | |
| " Total: $0.148192\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "response = client.images.edit(\n", | |
| " model=\"gpt-image-1.5\",\n", | |
| " image=open(\"product1.jpg\", \"rb\"),\n", | |
| " prompt=prompt,\n", | |
| " size=\"1024x1024\",\n", | |
| " n=1\n", | |
| ")\n", | |
| "image_data = base64.b64decode(response.data[0].b64_json)\n", | |
| "Path(\"product_darkbg.png\").write_bytes(image_data)\n", | |
| "print(\"β Saved to product_studio.png\")\n", | |
| "\n", | |
| "usage = response.usage\n", | |
| "print(\"π Token Usage:\")\n", | |
| "print(f\" Input tokens: {usage.input_tokens}\")\n", | |
| "print(f\" Output tokens: {usage.output_tokens}\")\n", | |
| "print(f\" Total tokens: {usage.total_tokens}\")\n", | |
| "\n", | |
| "input_cost = (usage.input_tokens / 1_000_000) * 8.00 # $8 per 1M input tokens\n", | |
| "output_cost = (usage.output_tokens / 1_000_000) * 32.00 # $32 per 1M output tokens\n", | |
| "total_cost = input_cost + output_cost\n", | |
| "\n", | |
| "print(f\"\\nπ° Estimated Cost:\")\n", | |
| "print(f\" Input: ${input_cost:.6f}\")\n", | |
| "print(f\" Output: ${output_cost:.6f}\")\n", | |
| "print(f\" Total: ${total_cost:.6f}\")" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.9.4" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment