Skip to content

Repository files navigation

image-gen-scripts

Scripts for generating images with the Google Gemini API and optimizing them for the web with Sharp.

What it does

  • 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.

Setup

  1. Install dependencies:

    npm install
  2. Get a Gemini API key from Google AI Studio.

  3. Copy .env.example to .env and set your key:

    cp .env.example .env

    In .env:

    GEMINI_API_KEY=your_key_here
    

    The generate script auto-loads .env and reads GEMINI_API_KEY (or GOOGLE_API_KEY).

Usage

Generate images (Gemini)

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 2K

Edit an existing image:

npm run generate -- --prompt "Make it sepia" --image path/to/photo.jpg

Interactive prompt (no --prompt): the script will ask for a prompt:

npm run generate

Options:

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.

Optimize images (Sharp)

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.png

Example:

npm run optimize-image -- ./generated/my-prompt-1234567890.png

Output: 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.

License

MIT

About

Scripts for generating images with the Google Gemini API and optimizing them for the web with Sharp.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages