How to crop an image to a circle in R with {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.
Hex sticker for the cropcircles R package

I hadn’t found a super-easy-you-don’t-have-to-think-about-it way to crop an image to a circle in R with a transparent background. There’s this stack overflow but I wouldn’t call it straightforward. So, I wrote this small package to do what I wanted.

In a nutshell, you pass a vector of image paths, which can be either local or from a URL link to circle_crop, and it will

  1. Download the images if required
  2. Crop the image to a circle with a transparent background,
  3. Return the path where the cropped images are saved

circle_crop can save the images to a specific location for use later, or if no location is given it will save them to a temporary location which is cleared when the session ends. All of this can happen in a single mutate step, so yeah, you don’t have to think about it. The cropped images can then be plotted with ggimageggpath, etc, or included in tables using e.g. gtreactable, etc.

Installation

Firstly, install from Git.

devtools::install_github("doehm/cropcircles")

Example

Here’s a quick example. This demonstrates how circle_crop will …

  1. Download the images from Open Psychometrics
  2. Circle crop the images and
  3. Make them available to plot on a chart

All in a few lines.

library(cropcircles)
library(dplyr)
library(ggimage)

images <- c(
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/1.jpg",
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/3.jpg",
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/9.jpg",
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/8.jpg")

df <- tibble(y = 1:4, images = images) |>
    mutate(images_cropped = circle_crop(images)) # where the magic happens

df |>
    ggplot() +
    geom_image(aes(1.5, y, image = images), size = 0.15) +
    geom_image(aes(3.5, y, image = images_cropped), size = 0.15) +
    xlim(0, 5) +
    ylim(0, 5) +
    coord_fixed()

The circle_crop function can take an image with any dimensions. It will circle crop the image from the center with a diameter of the smallest dimension.

The post How to crop an image to a circle in R with {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)