Memperluas konten gambar menggunakan perluasan

Halaman ini menjelaskan perluasan gambar. Outpainting memungkinkan Anda menggunakan Imagen untuk memperluas konten gambar ke area yang lebih besar atau area dengan dimensi yang berbeda.

Contoh perluasan gambar

Perluasan gambar adalah metode pengeditan berbasis mask yang memungkinkan Anda memperluas konten gambar dasar agar sesuai dengan kanvas mask yang lebih besar atau berukuran berbeda.

contoh gambar dasar
Gambar asli dengan padding gambar agar sesuai dengan ukuran gambar mask (target).
Sumber gambar: Kari Shea di Unsplash.
contoh gambar mask
Masker gambar dengan dimensi output target, dengan dimensi dan lokasi piksel gambar asli ditandai.
contoh gambar output
Gambar output perluasan (tanpa perintah).

Melihat kartu model Imagen untuk Pengeditan dan Penyesuaian

Sebelum memulai

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

  8. Siapkan autentikasi untuk lingkungan Anda.

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    Java

    Untuk menggunakan contoh Java di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

    1. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal dalam dokumentasi autentikasi Google Cloud .

    Node.js

    Untuk menggunakan contoh Node.js di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

    1. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal dalam dokumentasi autentikasi Google Cloud .

    Python

    Untuk menggunakan contoh Python di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

    1. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. If you're using a local shell, then create local authentication credentials for your user account:

      gcloud auth application-default login

      You don't need to do this if you're using Cloud Shell.

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal dalam dokumentasi autentikasi Google Cloud .

    REST

    Untuk menggunakan contoh REST API di halaman ini dalam lingkungan pengembangan lokal, gunakan kredensial yang Anda berikan ke gcloud CLI.

      After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    Untuk mengetahui informasi selengkapnya, lihat Melakukan autentikasi untuk menggunakan REST dalam dokumentasi autentikasi Google Cloud .

    Memperluas konten gambar

    Gunakan contoh kode berikut untuk memperluas konten gambar yang sudah ada.

    Imagen 3

    Gunakan contoh berikut untuk mengirim permintaan perluasan gambar menggunakan model Imagen 3.

    Konsol

    1. Di Google Cloud konsol, buka halaman Vertex AI > Media Studio .

      Buka Media Studio

    2. Klik Upload. Di dialog file yang ditampilkan, pilih file yang akan diupload.
    3. Klik Perluasan.
    4. Di menu Outpaint, pilih salah satu rasio aspek yang telah ditentukan sebelumnya untuk gambar akhir Anda, atau klik Kustom untuk menentukan dimensi kustom untuk gambar akhir Anda.
    5. Di toolbar pengeditan, pilih penempatan gambar Anda:
      • Rata kiri:
      • Penyejajaran tengah horizontal:
      • Rata kanan:
      • Sejajarkan atas:
      • Penyejajaran tengah vertikal:
      • Sejajarkan bawah:
    6. Opsional: Di panel Parameters, sesuaikan opsi berikut:
      • Model: model Imagen yang akan digunakan
      • Jumlah hasil: jumlah hasil yang akan dibuat
      • Perintah negatif: item yang harus dihindari pembuatannya
    7. Pada kolom perintah, masukkan perintah untuk mengubah gambar.
    8. Klik Generate.

    Python

    Instal

    pip install --upgrade google-genai

    Untuk mempelajari lebih lanjut, lihat dokumentasi referensi SDK.

    Tetapkan variabel lingkungan untuk menggunakan Gen AI SDK dengan Vertex AI:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=us-central1
    export GOOGLE_GENAI_USE_VERTEXAI=True

    from google import genai
    from google.genai.types import RawReferenceImage, MaskReferenceImage, MaskReferenceConfig, EditImageConfig
    
    client = genai.Client()
    
    # TODO(developer): Update and un-comment below line
    # output_file = "output-image.png"
    
    raw_ref = RawReferenceImage(
        reference_image=Image.from_file(location='test_resources/living_room.png'), reference_id=0)
    mask_ref = MaskReferenceImage(
        reference_id=1,
        reference_image=Image.from_file(location='test_resources/living_room_mask.png'),
        config=MaskReferenceConfig(
            mask_mode="MASK_MODE_USER_PROVIDED",
            mask_dilation=0.03,
        ),
    )
    
    image = client.models.edit_image(
        model="imagen-3.0-capability-001",
        prompt="A chandelier hanging from the ceiling",
        reference_images=[raw_ref, mask_ref],
        config=EditImageConfig(
            edit_mode="EDIT_MODE_OUTPAINT",
        ),
    )
    
    image.generated_images[0].image.save(output_file)
    
    print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
    # Example response:
    # Created output image using 1234567 bytes
    

    REST

    Untuk mengetahui informasi selengkapnya, lihat referensi API Edit gambar.

    Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

    • PROJECT_ID: Google Cloud Project ID Anda.
    • LOCATION: Region project Anda. Misalnya, us-central1, europe-west2, atau asia-northeast3. Untuk mengetahui daftar region yang tersedia, lihat Lokasi AI Generatif di Vertex AI.
    • prompt: Untuk perluasan gambar, Anda dapat memberikan string kosong untuk membuat gambar yang diedit. Jika Anda memilih untuk memberikan perintah, gunakan deskripsi area yang ditutupi untuk mendapatkan hasil terbaik. Misalnya, "langit biru", bukan "masukkan langit biru".
    • referenceType: ReferenceImage adalah gambar yang memberikan konteks tambahan untuk pengeditan gambar. Gambar referensi mentah RGB normal (REFERENCE_TYPE_RAW) diperlukan untuk kasus penggunaan pengeditan. Maksimal satu gambar referensi mentah dapat ada dalam satu permintaan. Gambar output memiliki tinggi dan lebar yang sama dengan gambar referensi mentah. Gambar referensi mask (REFERENCE_TYPE_MASK) diperlukan untuk kasus penggunaan pengeditan dengan mask. Jika ada gambar referensi mentah, gambar mask harus memiliki tinggi dan lebar yang sama dengan gambar referensi mentah. Jika gambar referensi mask kosong dan maskMode tidak disetel ke MASK_MODE_USER_PROVIDED, mask dihitung berdasarkan gambar referensi mentah.
    • B64_BASE_IMAGE: Gambar dasar yang akan diedit atau di-upscale. Gambar harus ditentukan sebagai string byte berenkode base64. Batas ukuran: 10 MB.
    • B64_OUTPAINTING_MASK: Gambar hitam putih yang ingin Anda gunakan sebagai lapisan mask untuk mengedit gambar asli. Masker harus memiliki resolusi yang sama dengan gambar input. Gambar output akan memiliki resolusi yang sama dengan gambar input. Gambar mask ini harus ditentukan sebagai string byte berenkode base64. Batas ukuran: 10 MB.
    • MASK_DILATION - float. Persentase lebar gambar untuk memperlebar masker ini. Nilai 0.03 direkomendasikan untuk perluasan gambar. Menyetel "dilation": 0.0 dapat menghasilkan batas yang jelas di titik ekstensi, atau dapat menyebabkan efek batas putih.
    • EDIT_STEPS - bilangan bulat. Jumlah langkah pengambilan sampel untuk model dasar. Untuk outpainting, mulai dari 35 langkah. Tingkatkan langkah jika kualitasnya tidak memenuhi persyaratan Anda.
    • EDIT_IMAGE_COUNT - Jumlah gambar yang diedit. Nilai bilangan bulat yang diterima: 1-4. Nilai default: 4.

    Metode HTTP dan URL:

    POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict

    Isi JSON permintaan:

    {
      "instances": [
        {
          "prompt": "",
          "referenceImages": [
            {
              "referenceType": "REFERENCE_TYPE_RAW",
              "referenceId": 1,
              "referenceImage": {
                "bytesBase64Encoded": "B64_BASE_IMAGE"
              }
            },
            {
              "referenceType": "REFERENCE_TYPE_MASK",
              "referenceId": 2,
              "referenceImage": {
                "bytesBase64Encoded": "B64_OUTPAINTING_MASK"
              },
              "maskImageConfig": {
                "maskMode": "MASK_MODE_USER_PROVIDED",
                "dilation": MASK_DILATION
              }
            }
          ]
        }
      ],
      "parameters": {
        "editConfig": {
          "baseSteps": EDIT_STEPS
        },
        "editMode": "EDIT_MODE_OUTPAINT",
        "sampleCount": EDIT_IMAGE_COUNT
      }
    }
    

    Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

    curl

    Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict"

    PowerShell

    Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict" | Select-Object -Expand Content
    Contoh respons berikut adalah untuk permintaan dengan "sampleCount": 2. Respons ini menampilkan dua objek prediksi, dengan byte gambar yang dihasilkan berenkode base64.
    {
      "predictions": [
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        },
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        }
      ]
    }
    

    Imagen 2

    Gunakan contoh berikut untuk mengirim permintaan perluasan gambar menggunakan model Imagen 2.

    Konsol

    1. Di Google Cloud konsol, buka halaman Vertex AI > Media Studio .

      Buka Media Studio

    2. Di panel tugas di bagian bawah, klik Edit gambar.

    3. Klik Upload untuk memilih gambar produk yang disimpan secara lokal yang akan diedit.

    4. Di toolbar pengeditan, klik Perluasan.

    5. Pilih salah satu rasio aspek yang telah ditentukan untuk gambar akhir Anda, atau klik Kustom untuk menentukan dimensi kustom untuk gambar akhir Anda.

    6. Opsional. Di toolbar pengeditan, pilih penempatan horizontal ( kiri, horizontal tengah, atau rata kanan) dan penempatan vertikal ( atas, vertikal tengah, atau rata bawah) gambar asli Anda di kanvas gambar yang akan dibuat.

    7. Opsional. Di panel Parameters, sesuaikan Number of results atau parameter lainnya.

    8. Klik Generate.

    Python

    Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Python.

    
    import vertexai
    from vertexai.preview.vision_models import Image, ImageGenerationModel
    
    # TODO(developer): Update and un-comment below lines
    # PROJECT_ID = "your-project-id"
    # input_file = "input-image.png"
    # mask_file = "mask-image.png"
    # output_file = "output-image.png"
    # prompt = "" # The optional text prompt describing what you want to see inserted.
    
    vertexai.init(project=PROJECT_ID, location="us-central1")
    
    model = ImageGenerationModel.from_pretrained("imagegeneration@006")
    base_img = Image.load_from_file(location=input_file)
    mask_img = Image.load_from_file(location=mask_file)
    
    images = model.edit_image(
        base_image=base_img,
        mask=mask_img,
        prompt=prompt,
        edit_mode="outpainting",
    )
    
    images[0].save(location=output_file, include_generation_parameters=False)
    
    # Optional. View the edited image in a notebook.
    # images[0].show()
    
    print(f"Created output image using {len(images[0]._image_bytes)} bytes")
    # Example response:
    # Created output image using 1234567 bytes
    

    REST

    Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

    • PROJECT_ID: Google Cloud Project ID Anda.
    • LOCATION: Region project Anda. Misalnya, us-central1, europe-west2, atau asia-northeast3. Untuk mengetahui daftar region yang tersedia, lihat Lokasi AI Generatif di Vertex AI.
    • prompt: Untuk perluasan gambar, Anda dapat memberikan string kosong untuk membuat gambar yang diedit.
    • B64_BASE_IMAGE: Gambar dasar yang akan diedit atau di-upscale. Gambar harus ditentukan sebagai string byte berenkode base64. Batas ukuran: 10 MB.
    • B64_OUTPAINTING_MASK: Gambar hitam putih yang ingin Anda gunakan sebagai lapisan mask untuk mengedit gambar asli. Masker harus memiliki resolusi yang sama dengan gambar input. Gambar output akan memiliki resolusi yang sama dengan gambar input. Gambar mask ini harus ditentukan sebagai string byte berenkode base64. Batas ukuran: 10 MB.
    • EDIT_IMAGE_COUNT: Jumlah gambar yang diedit. Nilai default: 4.

    Metode HTTP dan URL:

    POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@006:predict

    Isi JSON permintaan:

    {
      "instances": [
        {
          "prompt": "",
          "image": {
              "bytesBase64Encoded": "B64_BASE_IMAGE"
          },
          "mask": {
            "image": {
              "bytesBase64Encoded": "B64_OUTPAINTING_MASK"
            }
          }
        }
      ],
      "parameters": {
        "sampleCount": EDIT_IMAGE_COUNT,
        "editConfig": {
          "editMode": "outpainting"
        }
      }
    }
    

    Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

    curl

    Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@006:predict"

    PowerShell

    Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagegeneration@006:predict" | Select-Object -Expand Content
    Contoh respons berikut adalah untuk permintaan dengan "sampleCount": 2. Respons ini menampilkan dua objek prediksi, dengan byte gambar yang dihasilkan berenkode base64.
    {
      "predictions": [
        {
          "bytesBase64Encoded": "BASE64_IMG_BYTES",
          "mimeType": "image/png"
        },
        {
          "mimeType": "image/png",
          "bytesBase64Encoded": "BASE64_IMG_BYTES"
        }
      ]
    }
    

    Java

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Panduan memulai Vertex AI menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Java Vertex AI.

    Untuk melakukan autentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

    Dalam contoh ini, Anda menentukan model sebagai bagian dari EndpointName. EndpointName diteruskan ke metode predict yang dipanggil di PredictionServiceClient. Layanan ini menampilkan versi gambar yang telah diedit, yang kemudian disimpan secara lokal.

    
    import com.google.api.gax.rpc.ApiException;
    import com.google.cloud.aiplatform.v1.EndpointName;
    import com.google.cloud.aiplatform.v1.PredictResponse;
    import com.google.cloud.aiplatform.v1.PredictionServiceClient;
    import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
    import com.google.gson.Gson;
    import com.google.protobuf.InvalidProtocolBufferException;
    import com.google.protobuf.Value;
    import com.google.protobuf.util.JsonFormat;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Base64;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    
    public class EditImageOutpaintingMaskSample {
    
      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
        String projectId = "my-project-id";
        String location = "us-central1";
        String inputPath = "/path/to/my-input.png";
        String maskPath = "/path/to/my-mask.png";
        String prompt = ""; // The optional text prompt describing what you want to see inserted.
    
        editImageOutpaintingMask(projectId, location, inputPath, maskPath, prompt);
      }
    
      // Edit an image using a mask file. Outpainting lets you expand the content of a base image to fit
      // a larger or differently sized mask canvas.
      public static PredictResponse editImageOutpaintingMask(
          String projectId, String location, String inputPath, String maskPath, String prompt)
          throws ApiException, IOException {
        final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location);
        PredictionServiceSettings predictionServiceSettings =
            PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build();
    
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        try (PredictionServiceClient predictionServiceClient =
            PredictionServiceClient.create(predictionServiceSettings)) {
    
          final EndpointName endpointName =
              EndpointName.ofProjectLocationPublisherModelName(
                  projectId, location, "google", "imagegeneration@006");
    
          // Encode image and mask to Base64
          String imageBase64 =
              Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(inputPath)));
          String maskBase64 =
              Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(maskPath)));
    
          // Create the image and image mask maps
          Map<String, String> imageMap = new HashMap<>();
          imageMap.put("bytesBase64Encoded", imageBase64);
    
          Map<String, String> maskMap = new HashMap<>();
          maskMap.put("bytesBase64Encoded", maskBase64);
          Map<String, Map> imageMaskMap = new HashMap<>();
          imageMaskMap.put("image", maskMap);
    
          Map<String, Object> instancesMap = new HashMap<>();
          instancesMap.put("prompt", prompt); // [ "prompt", "<my-prompt>" ]
          instancesMap.put(
              "image", imageMap); // [ "image", [ "bytesBase64Encoded", "iVBORw0KGgo...==" ] ]
          instancesMap.put(
              "mask",
              imageMaskMap); // [ "mask", [ "image", [ "bytesBase64Encoded", "iJKDF0KGpl...==" ] ] ]
          instancesMap.put("editMode", "outpainting"); // [ "editMode", "outpainting" ]
          Value instances = mapToValue(instancesMap);
    
          // Optional parameters
          Map<String, Object> paramsMap = new HashMap<>();
          paramsMap.put("sampleCount", 1);
          Value parameters = mapToValue(paramsMap);
    
          PredictResponse predictResponse =
              predictionServiceClient.predict(
                  endpointName, Collections.singletonList(instances), parameters);
    
          for (Value prediction : predictResponse.getPredictionsList()) {
            Map<String, Value> fieldsMap = prediction.getStructValue().getFieldsMap();
            if (fieldsMap.containsKey("bytesBase64Encoded")) {
              String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue();
              Path tmpPath = Files.createTempFile("imagen-", ".png");
              Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded));
              System.out.format("Image file written to: %s\n", tmpPath.toUri());
            }
          }
          return predictResponse;
        }
      }
    
      private static Value mapToValue(Map<String, Object> map) throws InvalidProtocolBufferException {
        Gson gson = new Gson();
        String json = gson.toJson(map);
        Value.Builder builder = Value.newBuilder();
        JsonFormat.parser().merge(json, builder);
        return builder.build();
      }
    }
    

    Node.js

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai Vertex AI menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Node.js Vertex AI.

    Untuk melakukan autentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

    Dalam contoh ini, Anda memanggil metode predict pada PredictionServiceClient. Layanan ini menghasilkan gambar yang kemudian disimpan secara lokal.

    /**
     * TODO(developer): Update these variables before running the sample.
     */
    const projectId = process.env.CAIP_PROJECT_ID;
    const location = 'us-central1';
    const inputFile = 'resources/roller_skaters.png';
    const maskFile = 'resources/roller_skaters_mask.png';
    const prompt = 'city with skyscrapers';
    
    const aiplatform = require('@google-cloud/aiplatform');
    
    // Imports the Google Cloud Prediction Service Client library
    const {PredictionServiceClient} = aiplatform.v1;
    
    // Import the helper module for converting arbitrary protobuf.Value objects
    const {helpers} = aiplatform;
    
    // Specifies the location of the api endpoint
    const clientOptions = {
      apiEndpoint: `${location}-aiplatform.googleapis.com`,
    };
    
    // Instantiates a client
    const predictionServiceClient = new PredictionServiceClient(clientOptions);
    
    async function editImageOutpaintingMask() {
      const fs = require('fs');
      const util = require('util');
      // Configure the parent resource
      const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagegeneration@006`;
    
      const imageFile = fs.readFileSync(inputFile);
      // Convert the image data to a Buffer and base64 encode it.
      const encodedImage = Buffer.from(imageFile).toString('base64');
    
      const maskImageFile = fs.readFileSync(maskFile);
      // Convert the image mask data to a Buffer and base64 encode it.
      const encodedMask = Buffer.from(maskImageFile).toString('base64');
    
      const promptObj = {
        prompt: prompt, // The optional text prompt describing what you want to see inserted
        editMode: 'outpainting',
        image: {
          bytesBase64Encoded: encodedImage,
        },
        mask: {
          image: {
            bytesBase64Encoded: encodedMask,
          },
        },
      };
      const instanceValue = helpers.toValue(promptObj);
      const instances = [instanceValue];
    
      const parameter = {
        // Optional parameters
        seed: 100,
        // Controls the strength of the prompt
        // 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
        guidanceScale: 21,
        sampleCount: 1,
      };
      const parameters = helpers.toValue(parameter);
    
      const request = {
        endpoint,
        instances,
        parameters,
      };
    
      // Predict request
      const [response] = await predictionServiceClient.predict(request);
      const predictions = response.predictions;
      if (predictions.length === 0) {
        console.log(
          'No image was generated. Check the request parameters and prompt.'
        );
      } else {
        let i = 1;
        for (const prediction of predictions) {
          const buff = Buffer.from(
            prediction.structValue.fields.bytesBase64Encoded.stringValue,
            'base64'
          );
          // Write image content to the output file
          const writeFile = util.promisify(fs.writeFile);
          const filename = `output${i}.png`;
          await writeFile(filename, buff);
          console.log(`Saved image ${filename}`);
          i++;
        }
      }
    }
    await editImageOutpaintingMask();

    Batasan

    Model dapat menghasilkan detail yang terdistorsi jika gambar yang diperluas 200% atau lebih dari gambar asli. Sebagai praktik terbaik, sebaiknya Anda menambahkan langkah pasca-pemrosesan untuk menjalankan pencampuran alfa pada gambar yang di-outpaint.

    Kode berikut adalah contoh pasca-pemrosesan:

    parameters = {
       "editConfig": {
           "outpaintingConfig": {
             "blendingMode": "alpha-blending",
             "blendingFactor": 0.01,
           },
       },
    }
    
    

    Langkah berikutnya

    Baca artikel tentang Imagen dan produk AI Generatif lainnya di Vertex AI: