Struct rust_ai::openai::apis::image::Image

source ·
pub struct Image {
    pub image: Option<(String, Vec<u8>)>,
    pub mask: Option<(String, Vec<u8>)>,
    pub prompt: Option<String>,
    pub n: Option<u32>,
    pub size: Option<Size>,
    pub response_format: Option<Format>,
    pub user: Option<String>,
}
Expand description

Given a prompt and an instruction, the model will return an edited version of the prompt.

Fields§

§image: Option<(String, Vec<u8>)>

The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.

§mask: Option<(String, Vec<u8>)>

An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.

§prompt: Option<String>

A text description of the desired image(s). The maximum length is 1000 characters.

§n: Option<u32>

The number of images to generate. Must be between 1 and 10.

§size: Option<Size>

The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024. Use given enum variant.

§response_format: Option<Format>

The format in which the generated images are returned. Must be one of url or b64_json.

§user: Option<String>

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Implementations§

source§

impl Image

source

pub fn set_image(&mut self, filename: &str, bytes: Vec<u8>)

Set target image.

Arguments
  • filename - Image filename to edit or create variant
  • bytes - Image bytes to edit or create variant
Examples found in repository?
examples/image-edit.rs (lines 12-18)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("Make it cartoon-like.");
    image.set_image(
        "a.png",
        std::fs::read(std::path::PathBuf::from(
            "D:\\Contents\\Downloads\\WeChat Image_20230320174259.png",
        ))
        .unwrap(),
    );
    let result = image.editing().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}
More examples
Hide additional examples
examples/image-variant.rs (lines 12-18)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("Make it cartoon-like.");
    image.set_image(
        "a.png",
        std::fs::read(std::path::PathBuf::from(
            "D:\\Contents\\Downloads\\WeChat Image_20230320174259.png",
        ))
        .unwrap(),
    );
    let result = image.variation().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}
source

pub fn set_image_mask(&mut self, filename: &str, bytes: Vec<u8>)

Set target image mask.

Arguments
  • filename - Image filename to use as a mask
  • bytes - Image bytes to use as a mask
source

pub fn set_prompt(&mut self, content: &str)

Set target prompt for image generations.

Arguments
  • content - Target prompt
Examples found in repository?
examples/image-generation.rs (line 11)
5
6
7
8
9
10
11
12
13
14
15
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("A giant swan.");
    let result = image.generation().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}
More examples
Hide additional examples
examples/image-edit.rs (line 11)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("Make it cartoon-like.");
    image.set_image(
        "a.png",
        std::fs::read(std::path::PathBuf::from(
            "D:\\Contents\\Downloads\\WeChat Image_20230320174259.png",
        ))
        .unwrap(),
    );
    let result = image.editing().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}
examples/image-variant.rs (line 11)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("Make it cartoon-like.");
    image.set_image(
        "a.png",
        std::fs::read(std::path::PathBuf::from(
            "D:\\Contents\\Downloads\\WeChat Image_20230320174259.png",
        ))
        .unwrap(),
    );
    let result = image.variation().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}
source

pub async fn editing(&self) -> Result<ImageResponse, Box<dyn Error>>

Send edit request to OpenAI.

Examples found in repository?
examples/image-edit.rs (line 19)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("Make it cartoon-like.");
    image.set_image(
        "a.png",
        std::fs::read(std::path::PathBuf::from(
            "D:\\Contents\\Downloads\\WeChat Image_20230320174259.png",
        ))
        .unwrap(),
    );
    let result = image.editing().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}
source

pub async fn generation(&mut self) -> Result<ImageResponse, Box<dyn Error>>

Send generation request to OpenAI.

Examples found in repository?
examples/image-generation.rs (line 12)
5
6
7
8
9
10
11
12
13
14
15
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("A giant swan.");
    let result = image.generation().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}
source

pub async fn variation(&mut self) -> Result<ImageResponse, Box<dyn Error>>

Send variation request to OpenAI.

Examples found in repository?
examples/image-variant.rs (line 19)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async fn main() -> Result<(), Box<dyn Error>> {
    std::env::set_var("RUST_LOG", "debug");
    std::env::set_var("RUST_BACKTRACE", "1");
    log4rs::init_file("log4rs.yml", Default::default()).unwrap();

    let mut image = Image::default();
    image.set_prompt("Make it cartoon-like.");
    image.set_image(
        "a.png",
        std::fs::read(std::path::PathBuf::from(
            "D:\\Contents\\Downloads\\WeChat Image_20230320174259.png",
        ))
        .unwrap(),
    );
    let result = image.variation().await?;
    println!("{}", result.data.get(0).unwrap().url.clone().unwrap());
    Ok(())
}

Trait Implementations§

source§

impl Debug for Image

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Image

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Image

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for Image

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnwindSafe for Image

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · 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 Twhere U: From<T>,

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · 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
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,