C.W.K.
Stream
Lesson 04 of 04 · published

Image Gen & PDF Processing

~12 min · image-generation, pdf, multimodal

Level 0Spark
0 XP0/35 lessons0/10 achievements
0/140 XP to next level140 XP to go0% complete

Image generation lives in the same call

Switch to a preview image model and add response_modalities=['IMAGE']. The response's parts contain image bytes you can write to disk.

PDFs go through the File API

Upload PDFs the same way you upload images. Gemini parses text, tables, and (on 2.5+) extracts visual structure. Pages count toward token budget at roughly 250 tokens/page.

The boundary between image gen and image edit

Same model can do both. Send an image part + "replace the sky with stars" → you get a modified image back. Send only text → you get a generated image.

Code

Generate an image·python
from google import genai
from google.genai import types
from pathlib import Path

client = genai.Client()

response = client.models.generate_content(
    model='gemini-3-pro-image-preview',  # preview image model
    contents='A red-haired anime girl with blue eyes sitting at a desk full of code, '
             'soft warm light, oil painting style, cozy atmosphere.',
    config={'response_modalities': ['IMAGE']},
)

for part in response.parts:
    if part.mime_type and part.mime_type.startswith('image/'):
        Path('out.png').write_bytes(part.as_image().image_bytes)
        print('Wrote out.png')
PDF — extract structured info·python
import httpx, io

# Upload from local or from a URL
pdf_bytes = httpx.get('https://arxiv.org/pdf/2305.10601.pdf').content
doc = client.files.upload(
    file=io.BytesIO(pdf_bytes),
    config=dict(mime_type='application/pdf'),
)

response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents=[
        'Summarize this paper in 5 bullet points. Focus on the contribution and method.',
        doc,
    ],
)
print(response.text)

client.files.delete(name=doc.name)
Image edit — image in, image out·python
img_bytes = Path('original.png').read_bytes()

response = client.models.generate_content(
    model='gemini-3-pro-image-preview',
    contents=[
        types.Part.from_bytes(data=img_bytes, mime_type='image/png'),
        'Replace the cloudy sky with a starry night sky. Keep everything else exactly the same.',
    ],
    config={'response_modalities': ['IMAGE']},
)

for part in response.parts:
    if part.mime_type and part.mime_type.startswith('image/'):
        Path('edited.png').write_bytes(part.as_image().image_bytes)

External links

Exercise

Generate three images with the same prompt but different temperatures (0.2, 0.7, 1.2). Compare. Then take one of the generated images and ask the model to edit it (change a color, add an element, remove an element). Save before/after side-by-side. Note which edits were faithful and which weren't.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.