Generate images with OpenAI

R
OpenAI
Tutorial
Published

Feb 28, 2025

Introduction

In this tutorial, we will explore how to generate images using OpenAI’s image generation API in R. The openai package provides a simple way to interact with OpenAI’s models, making it easy for R users to create AI-generated visuals. Whether you’re a data scientist, researcher, or hobbyist, this guide will help you get started with generating images programmatically.

Setting Up the Environment

Install and Load the openai Package

Before we begin, make sure you have the openai package installed. If you haven’t installed it yet, you can do so using:

install.packages("openai", repos = "https://cloud.r-project.org/")

Load the package into your R session:

library(openai)

Setting Up Your API Key

To use OpenAI’s API, you need an API key. If you haven’t already, sign up on OpenAI’s websitea and obtain your API key. Then, store it securely in your R session:

Sys.setenv(OPENAI_API_KEY = "Your API Key")

Alternatively, you can store the key in an .Renviron file for persistent access.

Generating Images with OpenAI

Once you have set up your API key, you can start generating images. The create_image() function allows you to generate images based on text prompts.

Basic Image Generation

Here’s a simple example where we generate an image of a futuristic city:

response <- create_image(
  prompt = "a roman cat",
  n = 1,  # Number of images to generate
  size = "1024x1024"  # Image size
)

# Print the response to get the image URL
response$data$url

Customizing the Output

You can modify the n parameter to generate multiple images at once:

response <- create_image(
  prompt = "A cyberpunk-themed street with neon lights",
  n = 3,  # Generate three images
  size = "1024x1024"
)

# View all generated image URLs
response$data$url

Enhancing and Saving Images

Once you generate an image, you can download and save it using R. Here’s how:

library(httr)

# Download and save the first image
image_url <- response$data$url[1]
download.file(image_url, destfile = "generated_image.png", mode = "wb")

To render the downloaded image in your Quarto document, use the following code:

knitr::include_graphics("generated_image.png")

If you want to further process the image (e.g., cropping, resizing), you can use the magick package:

library(magick)

# Load and display the image
img <- image_read("generated_image.png")
image_display(img)

Use Cases and Conclusion

The ability to generate images with OpenAI in R opens up various possibilities:

  • Content Creation: Generate visuals for blogs, social media, and articles.
  • Data Visualization: Create illustrative images for reports and presentations.
  • Creative Exploration: Experiment with AI-driven art and design.