-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
125 lines (99 loc) · 3.79 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
error = FALSE,
message = FALSE,
warning = FALSE
)
```
# cuttingshapes
<!-- badges: start -->
<!-- badges: end -->
The goal of cuttingshapes is to make it easier to cut shapes out of images.
## Installation
You can install the development version of cuttingshapes from [github](https://github.com/annafergusson/cuttingshapes) with:
``` r
devtools:install_github("annafergusson/cuttingshapes")
```
## Example
This is a quick example of cutting a cat shape out of an image of pumpkins. Because it's Halloween!
```{r example1}
library(cuttingshapes)
cut_shape(image = "https://cdn.pixabay.com/photo/2019/09/08/19/01/pumpkin-4461665_1280.jpg",
shape = "https://cdn.pixabay.com/photo/2016/02/07/19/44/cat-1185453_960_720.png")
```
If you want to make the edges smoother, increase the fuzz factor from the default (but not too much!).
```{r example2}
cut_shape(image = "https://cdn.pixabay.com/photo/2019/09/08/19/01/pumpkin-4461665_1280.jpg",
shape = "https://cdn.pixabay.com/photo/2016/02/07/19/44/cat-1185453_960_720.png",
fuzz = 60)
```
With the `magick` package you can do some more cool things.
Maybe the image you want to use as the shape is not black and white?
First reduce the image down to two colours, then convert to black and white.
Oh yeah, you can also supply an image-magick external pointer object as an argument!
```{r example3}
library(magick)
pumpkins <- "https://cdn.pixabay.com/photo/2019/09/08/19/01/pumpkin-4461665_1280.jpg"
r_logo <- image_read("https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/1448px-R_logo.svg.png")
r_logo %>% image_quantize(max = 2) %>% image_convert(type = "bilevel") -> r_logo
cut_shape(image = pumpkins,
shape = r_logo)
```
You can also create some text to cut out of the pumpkins! Just make sure the background color is set when you create a blank image.
```{r example4}
word <- image_blank(width = 800,
height = 200,
) %>%
image_background("#FFFFFF") %>%
image_annotate("HALLOWEEN",
color = "#000000",
size = 100,
location = "+1+1")
cut_shape(image = pumpkins,
shape = word,
fuzz = 80)
```
Or make a hexagon shape cutter!
```{r example5}
radius <- 200
hex <- image_blank(800, 800) %>%
image_background("#ffffff") %>%
image_draw()
polygon(x = c(radius, radius + sin(pi/3)*radius, radius + sin(pi/3)*radius, radius, radius - sin(pi/3)*radius, radius - sin(pi/3)*radius),
y = c(0, cos(pi/3)*radius, 2*radius - cos(pi/3)*radius, radius*2, 2*radius - cos(pi/3)*radius, cos(pi/3)*radius),
col = "#000000")
nope <- dev.off()
cut_shape(image = pumpkins,
shape = hex,
fuzz = 80)
```
We can also take a plot and cut shapes using this. Note that this time, the colour to use for identifying the shapes to cut has been defined.
```{r example6}
library(tidyverse)
plot <- image_graph(width = 800,
height = 800,
res = 96)
data <- tibble(x = rep(1:10, 2),
rand_error = 10*runif(20),
y = 3*x + rand_error)
ggplot(data) +
geom_point(aes(x, y),
color = "#ff0000",
fill = "#ff0000",
size = 5)
nope <- dev.off()
cut_shape(image = pumpkins,
shape = plot,
color = "#ff0000",
fuzz = 30)
```
And we can use the animation features of `magick` to do even more scary stuff!
![](scary.gif)