# Grok Image Generator
A lightweight Elixir library for generating images based on text prompts using the Grok model. Simplifies image creation workflows within your Elixir applications.
## Installation
Add `grok_image_generator` to your list of dependencies in `mix.exs`:
elixir
def deps do
[
{:grok_image_generator, "~> 0.1.0"} # Replace with the actual version
]
end
Then, run `mix deps.get` to fetch the dependencies.
## Usage
This library provides a simple interface for interacting with the Grok image generation model. Remember to configure your API keys appropriately (see the library's configuration documentation for details).
**Example 1: Basic Image Generation**
elixir
alias GrokImageGenerator
prompt = "A futuristic cityscape at sunset"
{:ok, image_data} = GrokImageGenerator.generate_image(prompt)
# image_data now contains the raw image data (e.g., as a binary).
# You can then save it to a file or process it further.
# For example, saving to a file:
File.write!("image.png", image_data)
**Example 2: Handling Errors**
elixir
alias GrokImageGenerator
prompt = "This prompt will likely fail due to inappropriate content"
case GrokImageGenerator.generate_image(prompt) do
{:ok, image_data} ->
# Process the image data
File.write!("image.png", image_data)
{:error, reason} ->
IO.puts "Image generation failed: #{reason}"
end
**Example 3: Using Options for Customization**
elixir
alias GrokImageGenerator
prompt = "A portrait of a cat wearing a hat"
options = %{
resolution: "1024x1024",
style: "digital art"
}
{:ok, image_data} = GrokImageGenerator.generate_image(prompt, options)
File.write!("cat_portrait.png", image_data)
**Example 4: Image Generation with Pipe Operator**
elixir
alias GrokImageGenerator
"A serene landscape painting"
|> GrokImageGenerator.generate_image()
|> case do
{:ok, image_data} ->
File.write!("landscape.png", image_data)
{:error, reason} ->
IO.puts "Error generating image: #{reason}"
end
**Example 5: Extracting Image URL (if API returns it instead of the binary)**
(Adapt this example if the API returns a URL instead of raw binary data. If the API *always* returns binary, this example is not needed.)
elixir
alias GrokImageGenerator
alias HTTPoison
prompt = "A minimalist abstract design"
case GrokImageGenerator.generate_image(prompt) do
{:ok, %{url: image_url}} ->
# Assuming the API returns a JSON with a "url" field
{:ok, response} = HTTPoison.get(image_url)
File.write!("abstract.png", response.body)
{:ok, image_data} ->
File.write!("abstract.png", image_data) # Fallback, if API returns raw data.
{:error, reason} ->
IO.puts "Image generation failed: #{reason}"
end
## Feature Summary
* **Text-to-Image Generation:** Generates images directly from textual descriptions using the Grok model.
* **Simple API:** Easy-to-use functions for image creation within Elixir applications.
* **Error Handling:** Provides informative error messages for robust integration.
* **Customizable Options:** Supports options to tailor image generation, such as resolution and style (depending on the underlying API capabilities).
## License
MIT
This package is part of the grok-image-generator ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/blog/-grok-image-generator-model-on-supermaker-ai-twitterready-images-made-simple/