ในยุคที่ AI สร้างภาพกลายเป็นมาตรฐานอุตสาหกรรมแล้ว การต่อยอดด้วย inpainting (ลบวัตถุในภาพ) และ outpainting (ขยายภาพออกไป) ถือเป็นทักษะที่นักพัฒนาทุกคนต้องมี บทความนี้จะพาคุณเข้าใจ API การแก้ไขภาพด้วย AI อย่างละเอียด พร้อมโค้ดตัวอย่างที่รันได้จริง

กรณีการใช้งานจริงในอุตสาหกรรม

กรณีที่ 1: ร้านค้าอีคอมเมิร์ซ — ระบบอัปโหลดภาพสินค้าแบบอัตโนมัติ

ในการทำงานจริงที่ร้านค้าออนไลน์ ภาพสินค้ามักมีพื้นหลังที่ไม่เรียบร้อย โลโก้คู่แข่งติดมา หรือขนาดไม่เท่ากัน ทีมพัฒนาของเราเคยรับงานปรับปรุงระบบ CMS สำหรับแพลตฟอร์มอีคอมเมิร์ซรายใหญ่ ปัญหาคือต้องตัดภาพสินค้า 2,000 รายการต่อวัน แต่ทีมดีไซน์มีเพียง 3 คน

วิธีแก้คือใช้ outpainting API ขยายพื้นที่ภาพให้เป็นสี่เหลี่ยมจตุรัส จากนั้นใช้ inpainting API เติมพื้นหลังสีขาวให้เต็มพื้นที่ว่าง ผลลัพธ์คือลดเวลาประมวลผลจาก 8 ชั่วโมง/วัน เหลือเพียง 45 นาที คุณภาพภาพดีขึ้นด้วยเพราะ AI จัดการเรื่องแสงเงาอัตโนมัติ

กรณีที่ 2: องค์กรใหญ่ — ระบบ RAG สำหรับเอกสารภาพ

เมื่อองค์กรนำ RAG (Retrieval-Augmented Generation) มาใช้กับเอกสารที่มีทั้งตาราง แผนภูมิ และภาพประกอบ ความท้าทายคือบางภาพถูก scan มาเบลอหรือมีข้อความทับซ้อน ทีม data engineer ต้องสร้าง pipeline ที่ extract ข้อมูลจากภาพ แก้ไขคุณภาพด้วย AI ก่อนส่งเข้า vector database

วิธีการคือใช้ inpainting ลบข้อความที่ไม่เกี่ยวข้อง แล้วใช้ outpainting ขยายขอบภาพเพื่อให้ OCR ทำงานแม่นยำขึ้น ระบบที่ออกแบบมาสามารถประมวลผลเอกสาร 500 หน้า/ชั่วโมง โดยมีความแม่นยำของการอ่านข้อความเพิ่มขึ้น 23%

กรณีที่ 3: โปรเจกต์นักพัฒนาอิสระ — แอปตกแต่งภาพสำหรับโซเชียลมีเดีย

ในฐานะนักพัฒนาอิสระที่เคยสร้างแอปตกแต่งภาพสำหรับ content creator น้องหนึ่ง ปัญหาที่พบบ่อยคือผู้ใช้ต้องการเปลี่ยนพื้นหลังรูปถ่าย แต่ไม่มีทักษะ Photoshop วิธีที่ทำได้คือใช้ AI inpainting ตรวจจับวัตถุหลัก ลบพื้นหลังเดิม แล้วเติมพื้นหลังใหม่ที่ AI สร้างขึ้น

สิ่งที่ทำให้แอปประสบความสำเร็จคือความเร็วในการตอบสนอง ผู้ใช้คาดหวังว่าจะได้ภาพผลลัพธ์ภายใน 3 วินาที ซึ่ง สมัครที่นี่ เพื่อทดลอง API ที่มี latency ต่ำกว่า 50ms ได้เลย

พื้นฐาน Inpainting และ Outpainting API

Inpainting คือการแก้ไขส่วนที่เลือกในภาพ โดย AI จะสร้างเนื้อหาใหม่ที่เข้ากับบริบทรอบข้าง ส่วน Outpainting คือการขยายภาพออกไปในทิศทางที่กำหนด โดย AI จะสร้างเนื้อหาต่อเนื่องจากภาพเดิมอย่างเป็นธรรมชาติ

ความแตกต่างระหว่าง Inpainting กับ Outpainting

โค้ดตัวอย่าง: Python SDK

ด้านล่างคือโค้ด Python สำหรับเรียกใช้ Inpainting API ผ่าน HolySheep AI ซึ่งมีราคาประหยัดกว่า 85% เมื่อเทียบกับบริการอื่น ราคาเริ่มต้นเพียง $0.42/MTok สำหรับ DeepSeek V3.2

import base64
import requests
from io import BytesIO
from PIL import Image

การแก้ไขภาพด้วย Inpainting API

def inpaint_image(image_path: str, mask_path: str, prompt: str) -> Image.Image: """ ลบวัตถุออกจากภาพและเติมพื้นหลังอัตโนมัติ Args: image_path: ที่อยู่ไฟล์ภาพต้นฉบับ mask_path: ที่อยู่ไฟล์ mask (ส่วนที่ต้องการแก้ไข) prompt: คำอธิบายสิ่งที่ต้องการให้ AI สร้างแทน """ base_url = "https://api.holysheep.ai/v1" api_key = "YOUR_HOLYSHEEP_API_KEY" # โหลดและแปลงภาพเป็น base64 with open(image_path, "rb") as img_file: image_b64 = base64.b64encode(img_file.read()).decode("utf-8") with open(mask_path, "rb") as mask_file: mask_b64 = base64.b64encode(mask_file.read()).decode("utf-8") headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json" } payload = { "image": image_b64, "mask": mask_b64, "prompt": prompt, "model": "dalle-3-inpainting", "n": 1, "size": "1024x1024" } response = requests.post( f"{base_url}/images/edits", headers=headers, json=payload, timeout=30 ) if response.status_code == 200: result = response.json() # ดาวน์โหลดภาพผลลัพธ์ image_url = result["data"][0]["url"] image_response = requests.get(image_url) return Image.open(BytesIO(image_response.content)) else: raise Exception(f"Inpainting failed: {response.status_code} - {response.text}")

ตัวอย่างการใช้งาน

result_image = inpaint_image( image_path="product.jpg", mask_path="product_mask.png", prompt="white seamless background, studio lighting, professional photography" ) result_image.save("product_clean.png") print("✅ Inpainting เสร็จสมบูรณ์ ภาพถูกบันทึกที่ product_clean.png")

โค้ดตัวอย่าง: Outpainting สำหรับขยายภาพ

Outpainting ใช้ในกรณีที่ต้องการขยายภาพให้กว้างขึ้น เช่น ปรับภาพสินค้าให้เป็นสี่เหลี่ยมจตุรัสสำหรับแพลตฟอร์มอีคอมเมิร์ซ หรือสร้างภาพพาโนรามาจากภาพเดียว

import base64
import requests
from PIL import Image
import json

def outpaint_image(image_path: str, direction: str, extend_pixels: int, prompt: str) -> Image.Image:
    """
    ขยายภาพออกไปในทิศทางที่กำหนด
    
    Args:
        image_path: ที่อยู่ไฟล์ภาพต้นฉบับ
        direction: ทิศทางการขยาย (left/right/top/bottom/expand)
        extend_pixels: จำนวนพิกเซลที่ต้องการขยาย
        prompt: คำอธิบายสิ่งที่ต้องการให้ AI สร้างต่อ
    """
    base_url = "https://api.holysheep.ai/v1"
    api_key = "YOUR_HOLYSHEEP_API_KEY"
    
    # โหลดภาพต้นฉบับ
    original_image = Image.open(image_path)
    width, height = original_image.size
    
    # แปลงภาพเป็น base64
    buffered = BytesIO()
    original_image.save(buffered, format="PNG")
    image_b64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
    
    # คำนวณขนาดใหม่ตามทิศทาง
    if direction == "expand":
        new_width = width + (extend_pixels * 2)
        new_height = height + (extend_pixels * 2)
    elif direction in ["left", "right"]:
        new_width = width + extend_pixels
        new_height = height
    else:  # top or bottom
        new_width = width
        new_height = height + extend_pixels
    
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    payload = {
        "image": image_b64,
        "prompt": prompt,
        "model": "dalle-3-outpainting",
        "direction": direction,
        "extend_pixels": extend_pixels,
        "target_size": f"{new_width}x{new_height}"
    }
    
    response = requests.post(
        f"{base_url}/images/outpaint",
        headers=headers,
        json=payload,
        timeout=45
    )
    
    if response.status_code == 200:
        result = response.json()
        image_url = result["data"][0]["url"]
        image_response = requests.get(image_url)
        return Image.open(BytesIO(image_response.content))
    else:
        error_msg = response.json().get("error", {}).get("message", "Unknown error")
        raise Exception(f"Outpainting failed: {response.status_code} - {error_msg}")

ตัวอย่าง: ขยายภาพสินค้าให้เป็นสี่เหลี่ยมจตุรัส

product_image = outpaint_image( image_path="portrait_product.jpg", direction="expand", extend_pixels=512, prompt="continuation of the product photo, clean white background, soft studio lighting" ) product_image.save("square_product.png") print(f"✅ Outpainting เสร็จสมบูรณ์ ขนาดใหม่: {product_image.size}")

โค้ดตัวอย่าง: Batch Processing สำหรับระบบอีคอมเมิร์ซ

สำหรับร้านค้าที่ต้องประมวลผลภาพสินค้าจำนวนมาก ด้านล่างคือโค้ด batch processing ที่รวมทั้ง inpainting และ outpainting ใน pipeline เดียว ออกแบบมาสำหรับประมวลผลภาพสินค้าหลายพันรายการต่อวัน

import os
import concurrent.futures
import time
from dataclasses import dataclass
from typing import List, Optional

@dataclass
class ProductImage:
    """โครงสร้างข้อมูลสำหรับภา�