- Spec files
pypi-openai
Describes: pkg:pypi/openai@1.106.x
- Description
- Official Python library for the OpenAI API providing chat completions, embeddings, audio, images, and more
- Author
- tessl
- Last updated
images.md docs/
1# Images23Generate, edit, and create variations of images using DALL·E models with support for different sizes, quality levels, and style options.45## Capabilities67### Image Generation89Create images from text descriptions using DALL·E models with various customization options.1011```python { .api }12def generate(13self,14*,15prompt: str,16background: Optional[Literal["transparent", "opaque", "auto"]] | NotGiven = NOT_GIVEN,17model: Union[str, ImageModel, None] | NotGiven = NOT_GIVEN,18moderation: Optional[Literal["low", "auto"]] | NotGiven = NOT_GIVEN,19n: Optional[int] | NotGiven = NOT_GIVEN,20output_compression: Optional[int] | NotGiven = NOT_GIVEN,21output_format: Optional[Literal["png", "jpeg", "webp"]] | NotGiven = NOT_GIVEN,22partial_images: Optional[int] | NotGiven = NOT_GIVEN,23quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | NotGiven = NOT_GIVEN,24response_format: Optional[Literal["url", "b64_json"]] | NotGiven = NOT_GIVEN,25size: Optional[Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]] | NotGiven = NOT_GIVEN,26stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,27style: Optional[Literal["vivid", "natural"]] | NotGiven = NOT_GIVEN,28user: str | NotGiven = NOT_GIVEN,29# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.30# The extra values given here take precedence over values defined on the client or passed to this method.31extra_headers: Headers | None = None,32extra_query: Query | None = None,33extra_body: Body | None = None,34timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,35) -> ImagesResponse | Stream[ImageGenStreamEvent]: ...36```3738Usage examples:3940```python41from openai import OpenAI4243client = OpenAI()4445# Basic image generation46response = client.images.generate(47model="dall-e-3",48prompt="A futuristic cityscape with flying cars and neon lights",49size="1024x1024",50quality="standard",51n=152)5354image_url = response.data[0].url55print(f"Generated image URL: {image_url}")5657# High-definition image58response = client.images.generate(59model="dall-e-3",60prompt="A photorealistic portrait of a golden retriever wearing sunglasses",61size="1024x1024",62quality="hd",63style="natural"64)6566# Save image67import requests68from pathlib import Path6970image_url = response.data[0].url71image_response = requests.get(image_url)7273with open("golden_retriever.png", "wb") as f:74f.write(image_response.content)7576print("Image saved as golden_retriever.png")7778# Multiple images with DALL·E 279response = client.images.generate(80model="dall-e-2",81prompt="Abstract geometric patterns in bright colors",82size="512x512",83n=4 # Generate 4 variations84)8586for i, image_data in enumerate(response.data):87print(f"Image {i+1} URL: {image_data.url}")8889# Different sizes and styles90sizes = ["1024x1024", "1792x1024", "1024x1792"]91styles = ["vivid", "natural"]9293for size in sizes:94for style in styles:95response = client.images.generate(96model="dall-e-3",97prompt="A serene mountain landscape at sunset",98size=size,99style=style,100quality="standard"101)102103filename = f"mountain_{size}_{style}.png"104105# Download and save106image_url = response.data[0].url107image_response = requests.get(image_url)108with open(filename, "wb") as f:109f.write(image_response.content)110111print(f"Saved {filename}")112```113114### Base64 Image Handling115116Work with images in base64 format for direct integration and processing without external URLs.117118```python { .api }119def generate(120self,121*,122prompt: str,123response_format: Literal["b64_json"],124# ... other parameters125) -> ImagesResponse: ...126```127128Usage examples:129130```python131import base64132from io import BytesIO133from PIL import Image134135# Generate image as base64136response = client.images.generate(137model="dall-e-3",138prompt="A magical forest with glowing mushrooms",139size="1024x1024",140response_format="b64_json"141)142143# Get base64 data144base64_image = response.data[0].b64_json145146# Convert to image file147image_data = base64.b64decode(base64_image)148149# Save directly from base64150with open("magical_forest.png", "wb") as f:151f.write(image_data)152153# Open with PIL for processing154image_buffer = BytesIO(image_data)155pil_image = Image.open(image_buffer)156157# Process image158resized_image = pil_image.resize((512, 512))159resized_image.save("magical_forest_resized.png")160161print(f"Original size: {pil_image.size}")162print(f"Resized to: {resized_image.size}")163164# Generate multiple images as base64165response = client.images.generate(166model="dall-e-2",167prompt="Minimalist logo designs for a tech company",168size="256x256",169n=3,170response_format="b64_json"171)172173for i, image_data in enumerate(response.data):174# Decode and save each image175image_bytes = base64.b64decode(image_data.b64_json)176177with open(f"logo_design_{i+1}.png", "wb") as f:178f.write(image_bytes)179180print(f"Saved logo_design_{i+1}.png")181```182183### Image Editing184185Edit existing images by providing a mask to specify which areas to modify.186187```python { .api }188def edit(189self,190*,191image: Union[FileTypes, SequenceNotStr[FileTypes]],192prompt: str,193background: Optional[Literal["transparent", "opaque", "auto"]] | NotGiven = NOT_GIVEN,194input_fidelity: Optional[Literal["high", "low"]] | NotGiven = NOT_GIVEN,195mask: FileTypes | NotGiven = NOT_GIVEN,196model: Union[str, ImageModel, None] | NotGiven = NOT_GIVEN,197n: Optional[int] | NotGiven = NOT_GIVEN,198output_compression: Optional[int] | NotGiven = NOT_GIVEN,199output_format: Optional[Literal["png", "jpeg", "webp"]] | NotGiven = NOT_GIVEN,200partial_images: Optional[int] | NotGiven = NOT_GIVEN,201quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | NotGiven = NOT_GIVEN,202response_format: Optional[Literal["url", "b64_json"]] | NotGiven = NOT_GIVEN,203size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | NotGiven = NOT_GIVEN,204stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,205user: str | NotGiven = NOT_GIVEN,206# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.207# The extra values given here take precedence over values defined on the client or passed to this method.208extra_headers: Headers | None = None,209extra_query: Query | None = None,210extra_body: Body | None = None,211timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,212) -> ImagesResponse | Stream[ImageEditStreamEvent]: ...213```214215Usage examples:216217```python218# Edit image with mask219with open("original_image.png", "rb") as image_file, \220open("edit_mask.png", "rb") as mask_file:221222response = client.images.edit(223image=image_file,224mask=mask_file,225prompt="Replace the background with a beautiful sunset",226size="1024x1024",227n=2228)229230# Save edited images231for i, image_data in enumerate(response.data):232image_url = image_data.url233image_response = requests.get(image_url)234235with open(f"edited_image_{i+1}.png", "wb") as f:236f.write(image_response.content)237238# Edit without explicit mask (transparent areas will be edited)239with open("image_with_transparency.png", "rb") as image_file:240response = client.images.edit(241image=image_file,242prompt="Fill transparent areas with a starry night sky",243size="1024x1024"244)245246# Create mask programmatically and edit247from PIL import Image, ImageDraw248249# Open original image250original = Image.open("photo.jpg")251252# Create mask (white = edit area, black = keep original)253mask = Image.new("RGB", original.size, (0, 0, 0)) # Black background254draw = ImageDraw.Draw(mask)255256# Create circular mask in center257center_x, center_y = original.size[0] // 2, original.size[1] // 2258radius = min(original.size) // 4259260draw.ellipse(261[center_x - radius, center_y - radius, center_x + radius, center_y + radius],262fill=(255, 255, 255) # White circle263)264265# Save mask266mask.save("circular_mask.png")267268# Edit using programmatic mask269with open("photo.jpg", "rb") as image_file, \270open("circular_mask.png", "rb") as mask_file:271272response = client.images.edit(273image=image_file,274mask=mask_file,275prompt="Replace center area with a beautiful flower",276size="1024x1024"277)278```279280### Image Variations281282Create variations of existing images while maintaining similar style and composition.283284```python { .api }285def create_variation(286self,287*,288image: FileTypes,289model: Union[str, ImageModel, None] | NotGiven = NOT_GIVEN,290n: Optional[int] | NotGiven = NOT_GIVEN,291response_format: Optional[Literal["url", "b64_json"]] | NotGiven = NOT_GIVEN,292size: Optional[Literal["256x256", "512x512", "1024x1024"]] | NotGiven = NOT_GIVEN,293user: str | NotGiven = NOT_GIVEN,294# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.295# The extra values given here take precedence over values defined on the client or passed to this method.296extra_headers: Headers | None = None,297extra_query: Query | None = None,298extra_body: Body | None = None,299timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,300) -> ImagesResponse: ...301```302303Usage examples:304305```python306# Create variations of an existing image307with open("original_artwork.png", "rb") as image_file:308response = client.images.create_variation(309image=image_file,310n=4, # Create 4 variations311size="1024x1024"312)313314# Save all variations315for i, image_data in enumerate(response.data):316image_url = image_data.url317image_response = requests.get(image_url)318319with open(f"variation_{i+1}.png", "wb") as f:320f.write(image_response.content)321322print(f"Saved variation_{i+1}.png")323324# Create variations with different models325models = ["dall-e-2"] # Only DALL·E 2 supports variations currently326327for model in models:328with open("source_image.png", "rb") as image_file:329response = client.images.create_variation(330image=image_file,331model=model,332n=2,333size="512x512"334)335336for i, image_data in enumerate(response.data):337filename = f"{model}_variation_{i+1}.png"338339image_url = image_data.url340image_response = requests.get(image_url)341342with open(filename, "wb") as f:343f.write(image_response.content)344345# Generate variations as base64346with open("logo.png", "rb") as image_file:347response = client.images.create_variation(348image=image_file,349n=3,350size="256x256",351response_format="b64_json"352)353354for i, image_data in enumerate(response.data):355# Decode base64 and save356image_bytes = base64.b64decode(image_data.b64_json)357358with open(f"logo_variation_{i+1}.png", "wb") as f:359f.write(image_bytes)360```361362### Advanced Image Processing363364Combine image generation with advanced processing techniques and batch operations.365366Usage examples:367368```python369import concurrent.futures370from typing import List, Dict371import json372373# Batch image generation374def generate_image_batch(prompts: List[str], **kwargs) -> List[Dict]:375"""Generate multiple images concurrently"""376377def generate_single(prompt):378try:379response = client.images.generate(380prompt=prompt,381**kwargs382)383return {384"prompt": prompt,385"success": True,386"url": response.data[0].url,387"revised_prompt": getattr(response.data[0], 'revised_prompt', None)388}389except Exception as e:390return {391"prompt": prompt,392"success": False,393"error": str(e)394}395396# Use thread pool for concurrent requests397with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:398results = list(executor.map(generate_single, prompts))399400return results401402# Example batch generation403prompts = [404"A red sports car in the desert",405"A blue bird flying over mountains",406"A green forest with morning mist",407"A purple sunset over the ocean",408"A yellow sunflower field"409]410411batch_results = generate_image_batch(412prompts,413model="dall-e-3",414size="1024x1024",415quality="standard"416)417418# Process results419successful_generations = [r for r in batch_results if r["success"]]420failed_generations = [r for r in batch_results if not r["success"]]421422print(f"Successful: {len(successful_generations)}")423print(f"Failed: {len(failed_generations)}")424425# Save batch results metadata426with open("batch_results.json", "w") as f:427json.dump(batch_results, f, indent=2)428429# Image processing pipeline430def process_image_pipeline(prompt: str, output_dir: str = "output/"):431"""Complete image generation and processing pipeline"""432433from pathlib import Path434import os435436# Create output directory437Path(output_dir).mkdir(exist_ok=True)438439# Generate image440response = client.images.generate(441model="dall-e-3",442prompt=prompt,443size="1024x1024",444quality="hd",445response_format="b64_json"446)447448# Get image data449base64_image = response.data[0].b64_json450revised_prompt = getattr(response.data[0], 'revised_prompt', prompt)451452# Decode image453image_data = base64.b64decode(base64_image)454455# Save original456original_path = Path(output_dir) / "original.png"457with open(original_path, "wb") as f:458f.write(image_data)459460# Create thumbnails461image = Image.open(BytesIO(image_data))462463sizes = [(512, 512), (256, 256), (128, 128)]464for size in sizes:465thumbnail = image.copy()466thumbnail.thumbnail(size, Image.Resampling.LANCZOS)467468thumb_path = Path(output_dir) / f"thumbnail_{size[0]}x{size[1]}.png"469thumbnail.save(thumb_path)470471# Save metadata472metadata = {473"original_prompt": prompt,474"revised_prompt": revised_prompt,475"model": "dall-e-3",476"size": "1024x1024",477"quality": "hd",478"files": {479"original": str(original_path),480"thumbnails": [f"thumbnail_{s[0]}x{s[1]}.png" for s in sizes]481}482}483484metadata_path = Path(output_dir) / "metadata.json"485with open(metadata_path, "w") as f:486json.dump(metadata, f, indent=2)487488return metadata489490# Run pipeline491result = process_image_pipeline(492"A cyberpunk warrior in a neon-lit alley",493"cyberpunk_warrior/"494)495496print("Pipeline completed:")497print(json.dumps(result, indent=2))498499# Image style transfer using editing500def style_transfer_edit(source_image_path: str, style_prompt: str):501"""Use image editing for style transfer effects"""502503# Create a subtle mask for style transfer504source_image = Image.open(source_image_path)505506# Create mask with gradual opacity507mask = Image.new("L", source_image.size, 128) # 50% opacity everywhere508mask.save("style_mask.png")509510# Convert mask to RGB for API511mask_rgb = Image.new("RGB", source_image.size, (128, 128, 128))512mask_rgb.save("style_mask_rgb.png")513514# Perform style edit515with open(source_image_path, "rb") as image_file, \516open("style_mask_rgb.png", "rb") as mask_file:517518response = client.images.edit(519image=image_file,520mask=mask_file,521prompt=f"Transform the image with {style_prompt} style",522size="1024x1024"523)524525return response.data[0].url526527# Example style transfer528styled_url = style_transfer_edit(529"portrait.jpg",530"impressionist painting"531)532533print(f"Styled image: {styled_url}")534```535536## Types537538### Core Response Types539540```python { .api }541class ImagesResponse(BaseModel):542created: int543data: List[Image]544545class Image(BaseModel):546b64_json: Optional[str]547revised_prompt: Optional[str]548url: Optional[str]549```550551### Parameter Types552553```python { .api }554# Image generation parameters555ImageGenerateParams = TypedDict('ImageGenerateParams', {556'prompt': Required[str],557'background': NotRequired[Optional[Literal["transparent", "opaque", "auto"]]],558'model': NotRequired[Union[str, ImageModel, None]],559'moderation': NotRequired[Optional[Literal["low", "auto"]]],560'n': NotRequired[Optional[int]],561'output_compression': NotRequired[Optional[int]],562'output_format': NotRequired[Optional[Literal["png", "jpeg", "webp"]]],563'partial_images': NotRequired[Optional[int]],564'quality': NotRequired[Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]]],565'response_format': NotRequired[Optional[Literal["url", "b64_json"]]],566'size': NotRequired[Optional[Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]]],567'stream': NotRequired[Optional[bool]],568'style': NotRequired[Optional[Literal["vivid", "natural"]]],569'user': NotRequired[str],570'extra_headers': NotRequired[Headers],571'extra_query': NotRequired[Query],572'extra_body': NotRequired[Body],573'timeout': NotRequired[float],574}, total=False)575576# Image editing parameters577ImageEditParams = TypedDict('ImageEditParams', {578'image': Required[Union[FileTypes, SequenceNotStr[FileTypes]]],579'prompt': Required[str],580'background': NotRequired[Optional[Literal["transparent", "opaque", "auto"]]],581'input_fidelity': NotRequired[Optional[Literal["high", "low"]]],582'mask': NotRequired[FileTypes],583'model': NotRequired[Union[str, ImageModel, None]],584'n': NotRequired[Optional[int]],585'output_compression': NotRequired[Optional[int]],586'output_format': NotRequired[Optional[Literal["png", "jpeg", "webp"]]],587'partial_images': NotRequired[Optional[int]],588'quality': NotRequired[Optional[Literal["standard", "low", "medium", "high", "auto"]]],589'response_format': NotRequired[Optional[Literal["url", "b64_json"]]],590'size': NotRequired[Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]]],591'stream': NotRequired[Optional[bool]],592'user': NotRequired[str],593'extra_headers': NotRequired[Headers],594'extra_query': NotRequired[Query],595'extra_body': NotRequired[Body],596'timeout': NotRequired[float],597}, total=False)598599# Image variation parameters600ImageCreateVariationParams = TypedDict('ImageCreateVariationParams', {601'image': Required[FileTypes],602'model': NotRequired[Union[str, ImageModel, None]],603'n': NotRequired[Optional[int]],604'response_format': NotRequired[Optional[Literal["url", "b64_json"]]],605'size': NotRequired[Optional[Literal["256x256", "512x512", "1024x1024"]]],606'user': NotRequired[str],607'extra_headers': NotRequired[Headers],608'extra_query': NotRequired[Query],609'extra_body': NotRequired[Body],610'timeout': NotRequired[float],611}, total=False)612```613614### Model and Size Types615616```python { .api }617# Supported models618ImageModel = Literal["dall-e-2", "dall-e-3", "gpt-image-1"]619620# Image sizes621ImageSize = Literal[622"auto", # GPT-Image-1 default623"256x256", # DALL·E 2 only624"512x512", # DALL·E 2 only625"1024x1024", # All models626"1536x1024", # GPT-Image-1 landscape627"1024x1536", # GPT-Image-1 portrait628"1792x1024", # DALL·E 3 only629"1024x1792" # DALL·E 3 only630]631632# Quality options633ImageQuality = Literal[634"auto", # Default - automatic selection635"standard", # DALL·E 2/3636"hd", # DALL·E 3 only637"low", # GPT-Image-1 only638"medium", # GPT-Image-1 only639"high" # GPT-Image-1 only640]641642# Style options (DALL·E 3 only)643ImageStyle = Literal["vivid", "natural"]644645# Response formats646ResponseFormat = Literal["url", "b64_json"]647648# File types for input649FileTypes = Union[650bytes, # Raw image bytes651IO[bytes], # File-like object652str, # File path653os.PathLike[str] # Path object654]655656# Streaming types657ImageGenStreamEvent = Dict[str, Any]658ImageEditStreamEvent = Dict[str, Any]659Stream = Iterator[ImageGenStreamEvent]660SequenceNotStr = List[FileTypes]661```662663### Configuration and Limits664665```python { .api }666# Model capabilities and limits667class ImageModelLimits:668dall_e_2 = {669"sizes": ["256x256", "512x512", "1024x1024"],670"max_images": 10,671"supports_hd": False,672"supports_style": False,673"supports_variations": True,674"supports_editing": True,675"supports_streaming": False676}677678dall_e_3 = {679"sizes": ["1024x1024", "1792x1024", "1024x1792"],680"max_images": 1, # Only 1 image per request681"supports_hd": True,682"supports_style": True,683"supports_variations": False, # Not supported684"supports_editing": False, # Not supported685"supports_streaming": False686}687688gpt_image_1 = {689"sizes": ["auto", "1024x1024", "1536x1024", "1024x1536"],690"max_images": 10,691"supports_hd": False,692"supports_style": False,693"supports_variations": False,694"supports_editing": True,695"supports_streaming": True,696"supports_transparency": True,697"supports_compression": True,698"max_file_size": 50 * 1024 * 1024, # 50MB699"max_files": 16700}701702# Parameter constraints703class ImageConstraints:704prompt_max_length_dalle2: int = 1000 # characters705prompt_max_length_dalle3: int = 4000 # characters706prompt_max_length_gpt_image: int = 32000 # characters707708n_range_dalle2: Tuple[int, int] = (1, 10)709n_range_dalle3: Tuple[int, int] = (1, 1)710n_range_gpt_image: Tuple[int, int] = (1, 10)711712# Supported file formats for input713supported_formats_dalle2: List[str] = ["png"]714supported_formats_gpt_image: List[str] = ["png", "webp", "jpg"]715716max_file_size_dalle2: int = 4 * 1024 * 1024 # 4MB717max_file_size_gpt_image: int = 50 * 1024 * 1024 # 50MB718719# Compression range for GPT-Image-1720compression_range: Tuple[int, int] = (0, 100)721722# Partial images range for streaming723partial_images_range: Tuple[int, int] = (0, 3)724```725726## Best Practices727728### Prompt Engineering729730- Be specific and descriptive in prompts for better results731- Include style, mood, and technical details (lighting, composition, etc.)732- Use artistic style references (e.g., "in the style of Van Gogh")733- Specify image properties (photorealistic, illustration, sketch, etc.)734- Consider aspect ratio when choosing size735736### Model Selection737738- Use **GPT-Image-1** for advanced editing, streaming, transparency, and multiple input images739- Use **DALL·E 3** for highest quality single image generation with style control740- Use **DALL·E 2** for variations and basic editing capabilities741- Choose appropriate quality setting based on model capabilities742- Consider cost and feature trade-offs between models743744### Image Processing745746- Use base64 format for immediate processing without external downloads747- Implement proper error handling for generation failures748- Cache generated images to avoid regeneration costs749- Use appropriate image formats for your use case750751### Performance and Cost752753- Batch requests when possible to reduce API calls754- Use thumbnails and resizing for different display contexts755- Monitor usage and implement rate limiting for production756- Consider user experience with loading states for image generation757758### Safety and Content759760- Review generated content for appropriateness761- Implement content filtering based on your application's needs762- Be aware of OpenAI's usage policies for generated images763- Consider watermarking or attribution for generated content