Skip to content

rohitaryal/imageFX-api

Repository files navigation

imageFX-api (imagen)

Unofficial free reverse engineered api for imageFX(imagen) service provided by labs.google

Banner

Installation

npm i -g @rohitaryal/imagefx-api

Features

  1. Text to image using IMAGEN_3/4
  2. Image to text
  3. Command line support

Usage

imagefx can be invoked through both command line and as a module.

Command Line

Make sure you have:

  1. Installed imagefx globally (How to install?)

  2. Obtained your google account cookies (How to get cookies?)

  3. Set env variable GOOGLE_COOKIE containing your cookie

    Bash:

    export GOOGLE_COOKIE="__YOUR__COOKIE__HERE__"

    Command Prompt:

    set "GOOGLE_COOKIE=__YOUR__COOKIE__HERE__"

    Powershell:

    $GOOGLE_COOKIE = "__YOUR__GOOGLE__COOKIE__HERE__"

Basic Usages:

NOTE: If you are using environment variables, keep the quotes around cookie to avoid word-splitting and authentication errors.

  • Linux/macOS: "$GOOGLE_COOKIE"
  • PowerShell: "$env:GOOGLE_COOKIE"
  • Command Prompt: "%GOOGLE_COOKIE%"
  • Generating image with prompt

    # saves generated image at current directory
    imagefx generate --prompt "A bad friend" --cookie "$GOOGLE_COOKIE"
  • Selecting a specific model

    # please refer to --help for listing all models
    imagefx generate --prompt "An evil company" --model "IMAGEN_3_5" --cookie "$GOOGLE_COOKIE"
  • Selecting a specific aspect ratio

    # please refer to --help for listing all aspect ratio
    imagefx generate --prompt "Reptillian CEO" --size "PORTRAIT" --cookie "$GOOGLE_COOKIE"
  • Saving to specific destination

    # it will automatically create non-existing directory if possible
    imagefx generate --prompt "Netflix but with less fees" --dir ~/Pictures --cookie "$GOOGLE_COOKIE"
  • You can also save image using its media id.

    imagefx fetch "__MEDIA__ID__HERE__" --cookie "$GOOGLE_COOKIE"
  • Generating prompt/caption using an image as reference.

    # supported image types: jpeg, jpg, jpe, png, gif, webp, svg, bmp, tiff, apng, avif (not tested with all)
    imagefx caption --image /path/to/img.webp --type WEBP --cookie "$GOOGLE_COOKIE"

Full generation help:

imagefx generate <options>

Options:
      --version     Show version number
  -h, --help        Show help
  -p, --prompt      Textual description of image to be generated
  -m, --model       Model to be used for image generation
  -n, --count       Number of images to generate
      --size, --sz  Aspect ratio of image to be generated
  -s, --seed        Seed value for image to be generated
  -r, --retry       Number of retries if in case fetch fails
  -d, --dir         Directory to save generated images
  -c, --cookie      Google account cookie

Full caption generation help:

Generate detailed caption(s) from image

Options:
  --version  Show version number
  -h, --help     Show help
  -i, --image    Path to the image to be captioned
  -t, --type     Type of image (eg: png, jpeg, webp, etc)
  -n, --count    Number of captions to generate
  -c, --cookie   Google account cookie

Full fetching help:

imagefx fetch <mediaId>

Positionals:
  mediaId  Unique ID of generated image

Options:
      --version  Show version number
  -h, --help     Show help
  -d, --dir      Directory to save generated images
  -c, --cookie   Google account cookie
Importing as module
  • Basic image generation

    import { ImageFX } from "@rohitaryal/imagefx-api";
    
    const fx = new ImageFX(process.env.GOOGLE_COOKIE);
    
    // Generate images
    const generatedImage = await fx.generateImage("A big black cockroach");
    
    // Iterate over multiple images and save
    generatedImage.forEach(image => {
        const savedPath = image.save(".cache/");
            console.log("[+] Image saved at: " + savedPath);
    });
  • More descriptive prompt

    const fx = new ImageFX(GOOGLE_COOKIE);
    
    const prompt = new Prompt({
        seed: 0,
        numberOfImages: 4,
        prompt: "A green spongebob",
        generationModel: "IMAGEN_3_5",
        aspectRatio: "IMAGE_ASPECT_RATIO_SQUARE",
    });
    
    // Generate images
    const generatedImage = await fx.generateImage(prompt);
    
    // Iterate over generated images and save
    generatedImage.forEach(image => {
        const savedPath = image.save(".cache/");
        console.log("[+] Image saved at: " + savedPath);
    });

More examples are at: /examples

Help

How to extract cookies?

Easy way:

  1. Install Cookie Editor extension in your browser.
  2. Open labs.google, make sure you are logged in
  3. Click on Cookie Editor icon from Extensions section.
  4. Click on Export -> Header String

Manual way:

  1. Open labs.google, make sure you are logged in
  2. Press CTRL + SHIFT + I to open console
  3. Click on Network tab at top of console
  4. Press CTRL + L to clear network logs
  5. Click CTRL + R to refresh page
  6. Click on image-fx which should be at top
  7. Goto Request Headers section and copy all the content of Cookie
ImageFX not available in your country?
  1. Install a free VPN (Windscribe, Proton, etc)
  2. Open labs.google and login
  3. From here follow the "How to extract cookie?" in HELP section (above).
  4. Once you have obtained this cookie, you don't need VPN anymore.
Not able to generate images?

Create an issue here. Make sure the pasted logs don't contain cookie or tokens.

Contributions

Contribution are welcome but ensure to pass all test cases and follow existing coding standard.

Disclaimer

This project demonstrates usage of Google's private API but is not affiliated with Google. Use at your own risk.

About

Unofficial reverse engineered free api for imageFX from labs.google

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •