Skip to main content
Glama

edit_image

Edit existing images using text prompts to modify content, adjust aspect ratios, and change sizes within the Gemini Image MCP server.

Instructions

기존 이미지를 프롬프트에 따라 편집합니다

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
imagePathYes편집할 원본 이미지 파일 경로
promptYes이미지 편집 지시사항
outputPathYes편집된 이미지를 저장할 파일 경로
aspectRatioNo출력 이미지 비율
imageSizeNo출력 이미지 크기 (기본: 1K)
modelNo사용할 모델

Implementation Reference

  • The core editImage function that implements image editing logic. It takes an image path, prompt, and options, reads the image, converts it to base64, sends it to the Gemini API with the edit prompt, and saves the edited result to disk.
    export async function editImage(imagePath, prompt, options = {}) {
      const ai = getAI();
      const model = options.model || DEFAULT_MODEL;
      const outputPath = resolveOutput(
        options.output || options.outputPath || "./edited.png"
      );
    
      const absImagePath = path.resolve(imagePath);
      if (!fs.existsSync(absImagePath)) {
        throw new Error(`이미지 파일을 찾을 수 없습니다: ${absImagePath}`);
      }
    
      const imageData = fs.readFileSync(absImagePath);
      const base64Image = imageData.toString("base64");
      const mimeType = getMimeTypeFromPath(absImagePath);
    
      const config = buildConfig(options);
    
      const contents = [
        {
          inlineData: {
            mimeType,
            data: base64Image,
          },
        },
        { text: prompt },
      ];
    
      try {
        const response = await ai.models.generateContent({
          model,
          contents,
          config,
        });
    
        const image = extractImageFromResponse(response);
        const text = extractTextFromResponse(response);
    
        if (!image) {
          const fallbackMsg = text || "편집된 이미지가 생성되지 않았습니다.";
          return { success: false, text: fallbackMsg, outputPath: null };
        }
    
        ensureDir(outputPath);
        const finalPath = outputPath.match(/\.\w+$/)
          ? outputPath
          : outputPath + getExtFromMimeType(image.mimeType);
    
        fs.writeFileSync(finalPath, Buffer.from(image.data, "base64"));
    
        return { success: true, outputPath: finalPath, text: text || "" };
      } catch (error) {
        if (isOverloadError(error)) {
          return {
            success: false,
            text: `API 과부하 에러 (${model}). model: "${FALLBACK_MODEL}" 옵션으로 다시 시도해보세요.`,
            outputPath: null,
            overloaded: true,
          };
        }
        throw error;
      }
    }
  • server.js:82-138 (registration)
    MCP tool registration for 'edit_image'. Defines the input schema using Zod (imagePath, prompt, outputPath, aspectRatio, imageSize, model) and the async handler that calls editImage and formats the response.
    server.tool(
      "edit_image",
      "기존 이미지를 프롬프트에 따라 편집합니다",
      {
        imagePath: z.string().describe("편집할 원본 이미지 파일 경로"),
        prompt: z.string().describe("이미지 편집 지시사항"),
        outputPath: z.string().describe("편집된 이미지를 저장할 파일 경로"),
        aspectRatio: z
          .enum(["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"])
          .optional()
          .describe("출력 이미지 비율"),
        imageSize: z
          .enum(["1K", "2K", "4K"])
          .optional()
          .describe("출력 이미지 크기 (기본: 1K)"),
        model: z
          .string()
          .optional()
          .describe("사용할 모델"),
      },
      async ({ imagePath, prompt, outputPath, aspectRatio, imageSize, model }) => {
        try {
          const result = await editImage(imagePath, prompt, {
            outputPath,
            aspectRatio,
            imageSize,
            model,
          });
    
          if (!result.success) {
            return {
              content: [{ type: "text", text: result.text }],
              isError: !result.overloaded,
            };
          }
    
          return {
            content: [
              {
                type: "text",
                text: `이미지가 편집되었습니다: ${result.outputPath}${result.text ? "\n\n" + result.text : ""}`,
              },
            ],
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `이미지 편집 실패: ${error.message}`,
              },
            ],
            isError: true,
          };
        }
      }
    );
  • server.js:9-9 (registration)
    Import statement that brings the editImage function from gemini-image.js into the server module.
    editImage,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jk7g14/gemini-image-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server