Skip to main content

GeneratedImage

Struct GeneratedImage 

Source
pub struct GeneratedImage {
    pub data: Vec<u8>,
    pub format: ImageFormat,
    pub provider: ImageProviderKind,
    pub metadata: GenerationMetadata,
}
Expand description

A generated image with its data and metadata.

Fields§

§data: Vec<u8>

Raw image bytes.

§format: ImageFormat

Image format.

§provider: ImageProviderKind

Provider that generated this image.

§metadata: GenerationMetadata

Generation metadata.

Implementations§

Source§

impl GeneratedImage

Source

pub fn new( data: Vec<u8>, format: ImageFormat, provider: ImageProviderKind, metadata: GenerationMetadata, ) -> Self

Creates a new generated image.

Source

pub fn from_bytes( data: Vec<u8>, provider: ImageProviderKind, metadata: GenerationMetadata, ) -> Result<Self>

Creates a new generated image, detecting format from magic bytes.

Source

pub fn validate_format(&self) -> bool

Validates that the image data matches the claimed format.

Source

pub fn detected_format(&self) -> Option<ImageFormat>

Returns the actual format detected from magic bytes.

Source

pub fn size(&self) -> usize

Returns the size of the image data in bytes.

Examples found in repository?
examples/generate_image.rs (line 19)
10async fn main() -> genviz::Result<()> {
11    let provider = GeminiProvider::builder().build()?;
12
13    let request = GenerationRequest::new("A golden retriever puppy playing in snow");
14    let image = provider.generate(&request).await?;
15
16    image.save("output.png")?;
17    println!(
18        "Generated image: {} bytes, format: {:?}",
19        image.size(),
20        image.format
21    );
22
23    Ok(())
24}
More examples
Hide additional examples
examples/edit_image.rs (line 24)
10async fn main() -> genviz::Result<()> {
11    let input_path = std::env::args()
12        .nth(1)
13        .expect("Usage: edit_image <input_image.png>");
14
15    let input_bytes = std::fs::read(&input_path)?;
16
17    let provider = GeminiProvider::builder().build()?;
18
19    let request = GenerationRequest::new("Make the colors more vibrant and add a warm sunset glow")
20        .with_input_image(input_bytes);
21
22    let image = provider.generate(&request).await?;
23    image.save("edited.png")?;
24    println!("Edited image saved to edited.png ({} bytes)", image.size());
25
26    Ok(())
27}
examples/multi_provider.rs (line 37)
11async fn main() -> genviz::Result<()> {
12    // Try to build whichever provider has a key available
13    let provider: Box<dyn ImageProvider> = if std::env::var("GOOGLE_API_KEY").is_ok() {
14        println!("Using Gemini provider");
15        Box::new(genviz::GeminiProvider::builder().build()?)
16    } else if std::env::var("BFL_API_KEY").is_ok() {
17        println!("Using Flux provider");
18        Box::new(genviz::FluxProvider::builder().build()?)
19    } else if std::env::var("XAI_API_KEY").is_ok() {
20        println!("Using Grok provider");
21        Box::new(genviz::GrokProvider::builder().build()?)
22    } else if std::env::var("OPENAI_API_KEY").is_ok() {
23        println!("Using OpenAI provider");
24        Box::new(genviz::OpenAiImageProvider::builder().build()?)
25    } else {
26        eprintln!("Set at least one API key environment variable.");
27        std::process::exit(1);
28    };
29
30    println!("Provider: {} ({:?})", provider.name(), provider.kind());
31
32    let request = GenerationRequest::new("A serene mountain lake at dawn");
33    let image = provider.generate(&request).await?;
34
35    let filename = format!("{:?}_output.png", provider.kind());
36    image.save(&filename)?;
37    println!("Saved to {} ({} bytes)", filename, image.size());
38
39    Ok(())
40}
Source

pub fn save(&self, path: impl AsRef<Path>) -> Result<()>

Saves the image to the specified path.

Examples found in repository?
examples/generate_image.rs (line 16)
10async fn main() -> genviz::Result<()> {
11    let provider = GeminiProvider::builder().build()?;
12
13    let request = GenerationRequest::new("A golden retriever puppy playing in snow");
14    let image = provider.generate(&request).await?;
15
16    image.save("output.png")?;
17    println!(
18        "Generated image: {} bytes, format: {:?}",
19        image.size(),
20        image.format
21    );
22
23    Ok(())
24}
More examples
Hide additional examples
examples/edit_image.rs (line 23)
10async fn main() -> genviz::Result<()> {
11    let input_path = std::env::args()
12        .nth(1)
13        .expect("Usage: edit_image <input_image.png>");
14
15    let input_bytes = std::fs::read(&input_path)?;
16
17    let provider = GeminiProvider::builder().build()?;
18
19    let request = GenerationRequest::new("Make the colors more vibrant and add a warm sunset glow")
20        .with_input_image(input_bytes);
21
22    let image = provider.generate(&request).await?;
23    image.save("edited.png")?;
24    println!("Edited image saved to edited.png ({} bytes)", image.size());
25
26    Ok(())
27}
examples/multi_provider.rs (line 36)
11async fn main() -> genviz::Result<()> {
12    // Try to build whichever provider has a key available
13    let provider: Box<dyn ImageProvider> = if std::env::var("GOOGLE_API_KEY").is_ok() {
14        println!("Using Gemini provider");
15        Box::new(genviz::GeminiProvider::builder().build()?)
16    } else if std::env::var("BFL_API_KEY").is_ok() {
17        println!("Using Flux provider");
18        Box::new(genviz::FluxProvider::builder().build()?)
19    } else if std::env::var("XAI_API_KEY").is_ok() {
20        println!("Using Grok provider");
21        Box::new(genviz::GrokProvider::builder().build()?)
22    } else if std::env::var("OPENAI_API_KEY").is_ok() {
23        println!("Using OpenAI provider");
24        Box::new(genviz::OpenAiImageProvider::builder().build()?)
25    } else {
26        eprintln!("Set at least one API key environment variable.");
27        std::process::exit(1);
28    };
29
30    println!("Provider: {} ({:?})", provider.name(), provider.kind());
31
32    let request = GenerationRequest::new("A serene mountain lake at dawn");
33    let image = provider.generate(&request).await?;
34
35    let filename = format!("{:?}_output.png", provider.kind());
36    image.save(&filename)?;
37    println!("Saved to {} ({} bytes)", filename, image.size());
38
39    Ok(())
40}
Source

pub fn to_base64(&self) -> String

Encodes the image data as base64.

Source

pub fn to_data_url(&self) -> String

Returns the image as a data URL.

Trait Implementations§

Source§

impl Clone for GeneratedImage

Source§

fn clone(&self) -> GeneratedImage

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GeneratedImage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more