图片生成(文本转图片)

import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

async function main() {

  const ai = new GoogleGenAI({});

  const prompt =
    "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme";

  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash-image",
    contents: prompt,
  });
  for (const part of response.candidates[0].content.parts) {
    if (part.text) {
      console.log(part.text);
    } else if (part.inlineData) {
      const imageData = part.inlineData.data;
      const buffer = Buffer.from(imageData, "base64");
      fs.writeFileSync("gemini-native-image.png", buffer);
      console.log("Image saved as gemini-native-image.png");
    }
  }
}

main();

图片修改(文字和图片转图片)

提醒:请确保您对上传的所有图片均拥有必要权利。 请勿生成会侵犯他人权利的内容,包括会欺骗、骚扰或伤害他人的视频或图片。使用此生成式 AI 服务时须遵守我们的《使用限制政策》。

import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

async function main() {

  const ai = new GoogleGenAI({});

  const imagePath = "path/to/cat_image.png";
  const imageData = fs.readFileSync(imagePath);
  const base64Image = imageData.toString("base64");

  const prompt = [
    { text: "Create a picture of my cat eating a nano-banana in a" +
            "fancy restaurant under the Gemini constellation" },
    {
      inlineData: {
        mimeType: "image/png",
        data: base64Image,
      },
    },
  ];

  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash-image",
    contents: prompt,
  });
  for (const part of response.candidates[0].content.parts) {
    if (part.text) {
      console.log(part.text);
    } else if (part.inlineData) {
      const imageData = part.inlineData.data;
      const buffer = Buffer.from(imageData, "base64");
      fs.writeFileSync("gemini-native-image.png", buffer);
      console.log("Image saved as gemini-native-image.png");
    }
  }
}

main();

Gimini设计出的效果

​​​​​​​luxury perfume bottle, identical to the provided reference image, black lacquer glass bottle with minimalist label, placed in a dark cinematic environment, soft dramatic lighting from the side, subtle reflections on glossy black surface, elegant shadows, premium fashion campaign style, dark atmosphere inspired by modern mythology, high-end cinematic product photography, PG-13, ultra realistic, no people

Gemini 可以通过对话方式生成和处理图片。您可以使用 Gemini 2.5 Flash(又称 Nano Banana)或 Gemini 3 Pro 预览版(又称 Nano Banana Pro)来处理文本、图片或两者兼而有之的内容。这样一来,您就可以以前所未有的掌控力来创建、修改和迭代视觉效果。

 热门原创推荐

AI工具类文章

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐