Skip to main content
Glama

convert_image

Convert images between PNG, JPEG, GIF, WebP, and ICO formats with adjustable quality settings for JPEG and WebP files.

Instructions

Convert an image to a different format (PNG, JPEG, GIF, WebP, or ICO).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
input_pathYesAbsolute path to the source image file
output_formatYesTarget format
qualityNoJPEG/WebP quality (1-100, default 90)
output_pathNoWhere to save the output (defaults to same directory as input)

Implementation Reference

  • index.js:99-129 (handler)
    The implementation of the 'convert_image' tool using MCP SDK and sharp for image processing.
    server.tool(
      "convert_image",
      "Convert an image to a different format (PNG, JPEG, GIF, WebP, or ICO).",
      {
        input_path: z.string().describe("Absolute path to the source image file"),
        output_format: z.enum(["png", "jpeg", "gif", "webp", "ico"]).describe("Target format"),
        quality: z.number().int().min(1).max(100).optional().describe("JPEG/WebP quality (1-100, default 90)"),
        output_path: z.string().optional().describe("Where to save the output (defaults to same directory as input)"),
      },
      async ({ input_path, output_format, quality = 90, output_path }) => {
        try {
          await fs.access(input_path);
          let outPath;
          if (output_format === "ico") {
            outPath = resolveOutputPath(input_path, "ico", output_path);
            const icoBuffer = await encodeIco(input_path);
            await fs.writeFile(outPath, icoBuffer);
          } else {
            outPath = resolveOutputPath(input_path, output_format === "jpeg" ? "jpg" : output_format, output_path);
            let pipeline = sharp(input_path).toFormat(output_format, { quality });
            await pipeline.toFile(outPath);
          }
          const stat = await fs.stat(outPath);
          return {
            content: [{ type: "text", text: JSON.stringify({ success: true, output_path: outPath, size_bytes: stat.size }) }],
          };
        } catch (err) {
          return { isError: true, content: [{ type: "text", text: `Error: ${err.message}` }] };
        }
      }
    );

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/sonic0002/imagic-mcp'

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