irudnyts / openai

An R package-wrapper around OpenAI API
https://irudnyts.github.io/openai/
Other
164 stars 28 forks source link

How can I see the image created by create_image #22

Closed ManuelSpinola closed 1 year ago

ManuelSpinola commented 1 year ago

How can I see the image created by create_image?

irudnyts commented 1 year ago

There are basically two ways:

1/ Via URL (recommended):

library(openai)
response <- create_image(
    prompt = "An astronaut riding a horse in a photorealistic style",
    n = 1,
    size = "256x256",
    response_format = "url"
)
# copy and paste the following link in your browser
response$data$url

# or display in R Viewer
library(magick)
astronaut <- image_read(response$data$url)
print(astronaut)

2/ Via Base64 encoding:

response <- create_image(
    prompt = "An astronaut riding a horse in a photorealistic style",
    size = "256x256",
    response_format = "b64_json"
)

image <- jsonlite::base64_dec(response$data$b64_json) %>%
    png::readPNG()

grid::grid.raster(image)
ManuelSpinola commented 1 year ago

Thank you very much. Very helpful.