How to generate a hex sticker with {openai} and {cropcircles}

[This article was first published on R Archives - Dan Oehm | Gradient Descending, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Every R package needs a hex sticker. Here’s how you can generate one in seconds using generative AI.

This is easy to do with the {openai} and {cropcircles} packages. {openai} is a wrapper for the OpenAI API which makes it super simple to send a prompt to DALL-E and return an image. {cropcircles} makes it simple to crop the image into a hex and add a border. You can essentially make whatever you want from more minimalist style hexes to photo realistic images.

Make the hex

Set up

Install {openai} and {cropcircles}.

install.packages("openai")
install.packages("cropcircles")

library(openai)
library(cropcircles)

Next, set up the OpenAI API. It’s pretty simple, just follow the steps outlined on the {openai} Github page. Set your API key.

Sys.setenv(OPENAI_API_KEY = '<your-key-goes-here>')

Check that the API works with a quick test.

x <- create_image('<your-image-prompt>')

If it worked it should return some data including the URL to the image. If you get an error saying something along the lines of hard limit reached you’ll need to go into the OpenAI settings and set up a payment method. Log into OpenAI, go to API > preferences > Manage account > Billing, and set up a payment method. Even if you don’t buy credits I think you need to do this for the API.

To view the image run,

library(magick)
image_read(x$data$url)

Make the hex

When you’re all set up, it’s time to make a hex!

Generate an image.

x <- create_image("a dumpster fire in the style of salvador dali with some melted clocks")

View your work of art.

image_read(x$data$url)

It’s also a good idea to save the image is well. I believe the URL for the generated image expires after a short time and you’ll never get that image back.

image_write(x$data$url, "dumpsterfire.png")

Choose some fonts and colours.

library(showtext)

# choose a font from Google Fonts
font_add_google("Bangers", "bangers")
font_add_google("Roboto", "rob")
showtext_auto()
ft <- "bangers"
ft1 <- "rob"
txt <- "black"

# fontawesome fonts (optional - this adds the git logo - download from https://fontawesome.com/)
font_add("fa-brands", regular = "fonts/fontawesome-free-6.2.0-web/webfonts/fa-brands-400.ttf")

# package name and githu repo
pkg_name <- "dumpster\nFire"
git_name <- "doehm/dumpsterFire"

Crop the image to a hex and add a border.

library(cropcircles)
img_cropped <- hex_crop(
  images = x$data$url,
  border_colour = "#107e54",
  border_size = 24
)

Plot the image and add text with ggplot2. I like this way because you can use ggplot2 as you normally would to annotate the hex. You have as much freedom as you want. The code below can be used as a template. The limits are fixed between 0 and 1 to make it easy to position the text and other elements.

library(tidyverse)
library(ggpath)
library(ggtext)
library(glue)

ggplot() +
  geom_from_path(aes(0.5, 0.5, path = img_cropped)) +

  # package name
  annotate("text", x = 0.5, y = 0.25, label = pkg_name, family = ft, size = 34,
    fontface = "bold", colour = txt, angle = 22, hjust = 0, lineheight = 0.25) +

  # add github - remove if not wanted
  annotate("richtext", x=0.46, y = 0.07, family = ft1, size = 10, angle = 15, colour = txt, hjust = 0,
    label = glue("<span style='font-family:fa-brands; color:{txt}'> </span> {git_name}"),
    label.color = NA, fill = NA) +

  xlim(0, 1) +
  ylim(0, 1) +
  theme_void() +
  coord_fixed()

ggsave("images/hex-dumpster-fire.png", height = 6, width = 6)

Finally, admire your work!

Gallery

Here are a few more that I’ve created. They’re all just fake packages with fake repos but at the very least I hope they spur some ideas.

Boilerplate code

Sys.setenv(OPENAI_API_KEY = '<your-key-goes-here>')

x <- create_image("a beautiful sunset as the sun falls behind tall snow capped mountains and a pine forest")

font_add_google("Barlow", "bar")
showtext_auto()
ft <- "bar"
txt <- "black"

pkg_name <- "Package"

img_cropped <- hex_crop(
  images = x$data$url,
  border_colour = txt,
  border_size = 24
)

ggplot() +
  geom_from_path(aes(0.5, 0.5, path = img_cropped)) +
  annotate("text", x = 0.5, y = 0.1, label = pkg_name, family = ft, size = 42, colour = txt,
           angle = 30, hjust = 0, fontface = "bold") +
  xlim(0, 1) +
  ylim(0, 1) +
  theme_void() +
  coord_fixed()

ggsave("hex.png", height = 6, width = 6)

The post How to generate a hex sticker with {openai} and {cropcircles} appeared first on Dan Oehm | Gradient Descending.

To leave a comment for the author, please follow the link and comment on their blog: R Archives - Dan Oehm | Gradient Descending.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)