Contoh Penggunaan (Bahasa Melayu)

Contoh praktikal dan potongan kod untuk pelbagai kes penggunaan

Contoh Penggunaan (Bahasa Melayu)

Dokumen ini menyediakan pelbagai contoh penggunaan QwenImageEdit SDK untuk melakukan operasi penyuntingan imej yang biasa. Setiap contoh termasuk coretan kod dan penerangan ringkas.

1. Menghilangkan Latar Belakang

Contoh ini menunjukkan cara menghilangkan latar belakang dari imej menggunakan fungsi removeBackground.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function removeImageBackground() {
  try {
    const result = await qwenImageEdit.image.removeBackground({
      image: 'https://example.com/image-with-background.jpg', // Gantikan dengan URL imej anda
      outputFormat: 'png', // Atau 'webp' untuk latar belakang telus
    });
    console.log('Imej dengan latar belakang dihilangkan:', result.imageUrl);
    // Anda boleh memuat turun atau memaparkan imej ini
  } catch (error) {
    console.error('Gagal menghilangkan latar belakang:', error);
  }
}

removeImageBackground();

2. Mengubah Saiz Imej

Contoh ini menunjukkan cara mengubah saiz imej kepada dimensi tertentu menggunakan fungsi resize.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function resizeImage() {
  try {
    const result = await qwenImageEdit.image.resize({
      image: 'https://example.com/large-image.image.jpg', // Gantikan dengan URL imej anda
      width: 800,
      height: 600,
    });
    console.log('Imej diubah saiz:', result.imageUrl);
  } catch (error) {
    console.error('Gagal mengubah saiz imej:', error);
  }
}

resizeImage();

3. Mengaplikasikan Penapis Imej

Contoh ini menunjukkan cara mengaplikasikan penapis (cth., skala kelabu) ke imej menggunakan fungsi applyFilter.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function applyImageFilter() {
  try {
    const result = await qwenImageEdit.image.applyFilter({
      image: 'https://example.com/color-image.jpg', // Gantikan dengan URL imej anda
      filter: 'grayscale', // Cuba juga 'sepia', 'blur', dsb.
    });
    console.log('Imej dengan penapis diaplikasikan:', result.imageUrl);
  } catch (error) {
    console.error('Gagal mengaplikasikan penapis:', error);
  }
}

applyImageFilter();

4. Menambah Teks ke Imej

Contoh ini menunjukkan cara menambah teks ke imej pada kedudukan tertentu dengan gaya yang ditentukan menggunakan fungsi addText.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function addTextToImage() {
  try {
    const result = await qwenImageEdit.image.addText({
      image: 'https://example.com/base-image.jpg', // Gantikan dengan URL imej anda
      text: 'Selamat Datang!',
      x: 50,
      y: 100,
      font: 'Arial',
      fontSize: 48,
      color: '#FF0000', // Merah
    });
    console.log('Imej dengan teks:', result.imageUrl);
  } catch (error) {
    console.error('Gagal menambah teks:', error);
  }
}

addTextToImage();

5. Melapiskan Imej Lain

Contoh ini menunjukkan cara melapiskan imej kedua di atas imej dasar menggunakan fungsi overlayImage.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function overlayImages() {
  try {
    const result = await qwenImageEdit.image.overlayImage({
      baseImage: 'https://example.com/background.jpg', // Gantikan dengan URL imej latar belakang anda
      overlayImage: 'https://example.com/logo.png', // Gantikan dengan URL imej overlay anda
      x: 10,
      y: 10,
      opacity: 0.8,
    });
    console.log('Imej dengan overlay:', result.imageUrl);
  } catch (error) {
    console.error('Gagal melapiskan imej:', error);
  }
}

overlayImages();

6. Memotong Imej

Contoh ini menunjukkan cara memotong bahagian tertentu dari imej menggunakan fungsi crop.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function cropImage() {
  try {
    const result = await qwenImageEdit.image.crop({
      image: 'https://example.com/full-image.jpg', // Gantikan dengan URL imej anda
      x: 50,
      y: 50,
      width: 200,
      height: 150,
    });
    console.log('Imej dipotong:', result.imageUrl);
  } catch (error) {
    console.error('Gagal memotong imej:', error);
  }
}

cropImage();

7. Memutar Imej

Contoh ini menunjukkan cara memutar imej mengikut sudut tertentu menggunakan fungsi rotate.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function rotateImage() {
  try {
    const result = await qwenImageEdit.image.rotate({
      image: 'https://example.com/image-to-rotate.jpg', // Gantikan dengan URL imej anda
      angle: 90, // Putar 90 darjah mengikut arah jam
    });
    console.log('Imej diputar:', result.imageUrl);
  } catch (error) {
    console.error('Gagal memutar imej:', error);
  }
}

rotateImage();

8. Menukar Format Imej

Contoh ini menunjukkan cara menukar imej dari satu format ke format lain (cth., JPG ke WEBP) menggunakan fungsi convertFormat.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function convertImageFormat() {
  try {
    const result = await qwenImageEdit.image.convertFormat({
      image: 'https://example.com/image.jpg', // Gantikan dengan URL imej anda
      targetFormat: 'webp', // Cuba juga 'png', 'jpg'
    });
    console.log('Imej ditukar:', result.imageUrl);
  } catch (error) {
    console.error('Gagal menukar format imej:', error);
  }
}

convertImageFormat();

9. Pemprosesan Batch

Contoh ini menunjukkan cara melakukan beberapa operasi penyuntingan imej dalam satu panggilan batch.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function batchProcessImages() {
  try {
    const results = await qwenImageEdit.image.batchProcess({
      operations: [
        {
          type: 'removeBackground',
          image: 'https://example.com/batch-image1.jpg',
          outputFormat: 'png',
        },
        {
          type: 'resize',
          image: 'https://example.com/batch-image2.jpg',
          width: 400,
          height: 300,
        },
      ],
    });
    results.forEach((result, index) => {
      console.log(`Hasil operasi ${index + 1}:`, result.imageUrl);
    });
  } catch (error) {
    console.error('Gagal memproses batch imej:', error);
  }
}

batchProcessImages();

10. Mendapatkan Metadata Imej

Contoh ini menunjukkan cara mendapatkan metadata (cth., dimensi, format) dari imej menggunakan fungsi getMetadata.

import { QwenImageEdit } from '@qwen-image-edit/sdk';

const qwenImageEdit = new QwenImageEdit({ apiKey: 'YOUR_API_KEY' });

async function getImageMetadata() {
  try {
    const metadata = await qwenImageEdit.image.getMetadata({
      image: 'https://example.com/any-image.jpg', // Gantikan dengan URL imej anda
    });
    console.log('Metadata imej:', metadata);
  } catch (error) {
    console.error('Gagal mendapatkan metadata imej:', error);
  }
}

getImageMetadata();