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 도 같은 호출 안에

Preview image model 로 전환 + response_modalities=['IMAGE'] 추가. Response 의 parts 가 disk 에 쓸 수 있는 image bytes 포함.

PDF 는 File API 통과

Image 와 같은 방식으로 PDF 업로드. Gemini 가 text, table parse, (2.5+ 에서) visual structure 추출. Pages 가 페이지당 약 250 토큰으로 token budget 에 카운트.

Image gen 과 image edit 의 경계

같은 모델이 둘 다 가능. Image part + "하늘을 별로 바꿔" 보내면 → 수정된 image 반환. Text 만 보내면 → 생성된 image 받음.

Code

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 — 구조화된 정보 추출·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 편집 — 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

같은 prompt 로 image 셋 생성, 다른 temperature (0.2, 0.7, 1.2). 비교. 그 다음 생성된 image 하나 잡아서 모델한테 편집 요청 (색 변경, 요소 추가, 요소 제거). Before/after 나란히 저장. 어느 편집이 충실하고 어느 게 아니었는지 기록.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.