Scripts for generating images with the Google Gemini API and optimizing them for the web with Sharp.
- Generate: Create or edit images using Gemini. Supports text-to-image and image-to-image (edit an existing image with a prompt). Outputs PNGs to a
generated/folder. - Optimize: Resize an image to multiple widths (600, 1200, 2400px), produce AVIF and WebP variants in
dist/, and print responsive<picture>HTML you can paste into your site.
-
Install dependencies:
npm install
-
Get a Gemini API key from Google AI Studio.
-
Copy
.env.exampleto.envand set your key:cp .env.example .env
In
.env:GEMINI_API_KEY=your_key_hereThe generate script auto-loads
.envand readsGEMINI_API_KEY(orGOOGLE_API_KEY).
Text-to-image:
npm run generate -- --prompt "A sunset over mountains"With aspect ratio and resolution:
npm run generate -- --prompt "A cat on a windowsill" --aspect-ratio 16:9 --resolution 2KEdit an existing image:
npm run generate -- --prompt "Make it sepia" --image path/to/photo.jpgInteractive prompt (no --prompt): the script will ask for a prompt:
npm run generateOptions:
| Option | Description | Default |
|---|---|---|
--prompt <text> |
Text prompt for generation or editing | (interactive if omitted) |
--image <path> |
Input image for editing (PNG, JPG, JPEG, WebP) | — |
--aspect-ratio <ratio> |
One of: 1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9 | 1:1 |
--resolution <size> |
One of: 1K, 2K, 4K | 1K |
-h, --help |
Show help | — |
Generated files are saved under generated/ with a slug from the prompt and a timestamp.
Run the optimizer on a single image (e.g. PNG or JPEG). It will create AVIF and WebP files at 600, 1200, and 2400px width in dist/ and print <picture> markup:
npm run optimize-image -- path/to/image.pngExample:
npm run optimize-image -- ./generated/my-prompt-1234567890.pngOutput: dist/<basename>-600.avif, dist/<basename>-600.webp, and same for 1200 and 2400. Only widths up to the image’s original width are generated. The script also prints responsive <picture> HTML you can use in your pages.
MIT