본문 바로가기
C.W.K.
Stream
Lesson 04 of 04 · published

이미지 생성과 PDF 처리

~12 min · image-generation, pdf, multimodal

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

이미지 생성도 같은 호출 안에서 해

미리보기 이미지 모델로 바꾸고 response_modalities=['IMAGE']를 추가해. 응답의 parts에는 디스크에 쓸 수 있는 이미지 바이트가 들어 있어.

PDF는 File API를 거쳐

이미지와 같은 방식으로 PDF를 올려. Gemini가 텍스트와 표를 파싱하고, 2.5 이상에서는 시각 구조도 추출해. 페이지는 한 장당 약 250토큰으로 토큰 예산에 포함돼.

이미지 생성과 편집의 경계

같은 모델이 둘 다 할 수 있어. 이미지 part와 "하늘을 별이 빛나는 밤으로 바꿔"라는 지시를 함께 보내면 수정된 이미지를 받고, 텍스트만 보내면 새로 생성한 이미지를 받아.

Code

이미지 생성·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)
이미지 편집 — 이미지를 넣고 이미지 받기·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

같은 프롬프트와 서로 다른 temperature(0.2, 0.7, 1.2)로 이미지 세 장을 만들어 비교해. 그다음 생성된 이미지 하나를 골라 색 바꾸기, 요소 추가, 요소 제거를 요청해. 편집 전후를 나란히 저장하고 어떤 편집은 지시를 잘 따랐고 어떤 편집은 그렇지 않았는지 기록해.

Progress

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

댓글 0

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

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