Repository Public
1#!/usr/bin/env python32# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project : API Atlas10# File : examples/xai/images/edit.py11# Description : Edit an image on xAI: JSON body with a base64 data URL12# (the OpenAI SDK images.edit() is NOT supported — it sends13# multipart).14# Module : examples15# Repository : https://git.spboucher.ai/doc-api16# Created : 2026-09-1917# Last Modified : 2026-09-1918#19# =============================================================================20# AUTHOR & PROFESSIONAL AFFILIATION21# =============================================================================22#23# Author : Simon-Pierre Boucher24# Position : Professor of Finance25# Institution : Université du Québec en Outaouais (UQO)26# Department : Département des sciences administratives27# Organization : SP Boucher28#29# Website : https://spboucher.ai30# Git : https://git.spboucher.ai31#32# =============================================================================33# OFFICIAL CONTACT DIRECTORY34# =============================================================================35#36# General : info@spboucher.ai37# Professional : contact@spboucher.ai38# Direct : simon@spboucher.ai39# Research : research@spboucher.ai40# Development : dev@spboucher.ai41# Support : support@spboucher.ai42# Security/Admin : admin@spboucher.ai43# Notifications : notifications@spboucher.ai44#45# Preferred Contact: contact@spboucher.ai46#47# =============================================================================48# COPYRIGHT & INTELLECTUAL PROPERTY49# =============================================================================50#51# Copyright (c) 202652# Simon-Pierre Boucher53#54# All rights reserved where applicable.55#56# This file is governed by the applicable project license,57# copyright notices, institutional agreements and relevant law.58#59# Unless expressly authorized by the applicable license or60# rights holder, copying, redistribution, sublicensing,61# publication and commercial exploitation are not permitted.62#63# Existing third-party rights and notices remain unaffected.64#65# =============================================================================66# CONFIDENTIALITY & SECURITY67# =============================================================================68#69# Classification : Public70# Distribution : Public repository; use is governed by the project license71# Security Contact : admin@spboucher.ai72#73# Access to this source does not independently grant permission74# to redistribute or disclose confidential materials.75#76# Do not embed credentials, private keys, access tokens,77# certificates or confidential personal information.78#79# =============================================================================80# LICENSE & MAINTENANCE81# =============================================================================82#83# License : None specified (no license file in this repository)84# Maintainer : Simon-Pierre Boucher85# Technical Contact: dev@spboucher.ai86#87# =============================================================================88# END OF SP BOUCHER SOURCE HEADER89# =============================================================================9091"""Edit an image on xAI: JSON body with a base64 data URL (the OpenAI SDK images.edit() is NOT supported — it sends multipart).9293STATUS: UNVERIFIED as a script (identical raw request LIVE_VERIFIED 2026-09-18: grok-imagine-image edit of a generated JPEG,94 "draw a small red circle in the center" -> 200, usage.cost_in_usd_ticks 220000000 = $0.022 = output $0.02 + input image $0.002).95Run: .venv/bin/python examples/xai/images/edit.py path/to/image.jpg ["prompt"]96"""97from __future__ import annotations9899import base64100import mimetypes101import sys102from pathlib import Path103104ROOT = Path(__file__).resolve().parents[3]105sys.path.insert(0, str(ROOT))106from scripts import live # noqa: E402107108src = Path(sys.argv[1] if len(sys.argv) > 1 else ROOT / "tmp-live" / "xai-example-generate.jpg")109prompt = sys.argv[2] if len(sys.argv) > 2 else "draw a small red circle in the center"110mime = mimetypes.guess_type(src.name)[0] or "image/jpeg"111data_url = f"data:{mime};base64," + base64.b64encode(src.read_bytes()).decode()112113st, body, _ = live.xai_request("POST", "/v1/images/edits", {114 "model": "grok-imagine-image",115 "prompt": prompt,116 "n": 1,117 "response_format": "b64_json",118 "image": {"url": data_url}, # or {"file_id": "file_..."} from the Files API; `images: [...]` for 2-5 references119}, est_cost_usd=0.022, note="example images/edit.py")120print("HTTP", st)121if st != 200:122 print(body)123 raise SystemExit(1)124out = ROOT / "tmp-live" / "xai-example-edit.jpg"125out.write_bytes(base64.b64decode(body["data"][0]["b64_json"]))126print({"mime_type": body["data"][0].get("mime_type"), "usage": body.get("usage")}, "saved", out)127