Skip to content

Commit

Permalink
Switch to base-R pipe in examples and vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Jan 28, 2024
1 parent 0708bb5 commit ba4c28b
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 76 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ Suggests:
tesseract (>= 2.0),
gifski
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
Roxygen: list(load = "installed", markdown = TRUE)
Language: en-US
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2.8.3
- Switch to base-R pipe in examples and vignette

2.8.2
- Fix image_draw() for small images: #317
- Change image_shadow() to transparent background by default
Expand Down
4 changes: 2 additions & 2 deletions R/edges.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#' @examples if(magick_config()$version > "6.8.9"){
#' shape <- demo_image("shape_rectangle.gif")
#' rectangle <- image_canny(shape)
#' rectangle %>% image_hough_draw('5x5+20')
#' rectangle %>% image_hough_txt(format = 'svg') %>% cat()
#' rectangle |> image_hough_draw('5x5+20')
#' rectangle |> image_hough_txt(format = 'svg') |> cat()
#' }
image_edge <- function(image, radius = 1){
assert_image(image)
Expand Down
32 changes: 16 additions & 16 deletions R/morphology.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,38 @@
#' @family image
#' @examples #example from IM website:
#' if(magick_config()$version > "6.8.8"){
#' pixel <- image_blank(1, 1, 'white') %>% image_border('black', '5x5')
#' pixel <- image_blank(1, 1, 'white') |> image_border('black', '5x5')
#'
#' # See the effect of Dilate method
#' pixel %>% image_scale('800%')
#' pixel %>% image_morphology('Dilate', "Diamond") %>% image_scale('800%')
#' pixel |> image_scale('800%')
#' pixel |> image_morphology('Dilate', "Diamond") |> image_scale('800%')
#'
#' # These produce the same output:
#' pixel %>% image_morphology('Dilate', "Diamond", iter = 3) %>% image_scale('800%')
#' pixel %>% image_morphology('Dilate', "Diamond:3") %>% image_scale('800%')
#' pixel |> image_morphology('Dilate', "Diamond", iter = 3) |> image_scale('800%')
#' pixel |> image_morphology('Dilate', "Diamond:3") |> image_scale('800%')
#'
#' # Plus example
#' pixel %>% image_morphology('Dilate', "Plus", iterations = 2) %>% image_scale('800%')
#' pixel |> image_morphology('Dilate', "Plus", iterations = 2) |> image_scale('800%')
#'
#' # Rose examples
#' rose %>% image_morphology('ErodeI', 'Octagon', iter = 3)
#' rose %>% image_morphology('DilateI', 'Octagon', iter = 3)
#' rose %>% image_morphology('OpenI', 'Octagon', iter = 3)
#' rose %>% image_morphology('CloseI', 'Octagon', iter = 3)
#' rose |> image_morphology('ErodeI', 'Octagon', iter = 3)
#' rose |> image_morphology('DilateI', 'Octagon', iter = 3)
#' rose |> image_morphology('OpenI', 'Octagon', iter = 3)
#' rose |> image_morphology('CloseI', 'Octagon', iter = 3)
#'
#' # Edge detection
#' man <- demo_image('man.gif')
#' man %>% image_morphology('EdgeIn', 'Octagon')
#' man %>% image_morphology('EdgeOut', 'Octagon')
#' man %>% image_morphology('Edge', 'Octagon')
#' man |> image_morphology('EdgeIn', 'Octagon')
#' man |> image_morphology('EdgeOut', 'Octagon')
#' man |> image_morphology('Edge', 'Octagon')
#'
#' # Octagonal Convex Hull
#' man %>%
#' image_morphology('Close', 'Diamond') %>%
#' man |>
#' image_morphology('Close', 'Diamond') |>
#' image_morphology('Thicken', 'ConvexHull', iterations = 1)
#'
#' # Thinning down to a Skeleton
#' man %>% image_morphology('Thinning', 'Skeleton', iterations = 1)
#' man |> image_morphology('Thinning', 'Skeleton', iterations = 1)
#'
#' # Specify custom kernel matrix usingn a string:
#' img <- demo_image("test_mag.gif")
Expand Down
8 changes: 4 additions & 4 deletions R/segmentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
#' \donttest{
#' # Split image in blobs of connected pixel levels
#' if(magick_config()$version > "6.9.0"){
#' objects %>%
#' image_connect(connectivity = 4) %>%
#' objects |>
#' image_connect(connectivity = 4) |>
#' image_split()
#'
#' # Fuzzy c-means
#' image_fuzzycmeans(logo)
#'
#' logo %>%
#' image_convert(colorspace = "HCL") %>%
#' logo |>
#' image_convert(colorspace = "HCL") |>
#' image_fuzzycmeans(smoothing = 5)
#' }
#' }
Expand Down
4 changes: 2 additions & 2 deletions R/threshold.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#' image_threshold(test, type = "white", threshold = "50%")
#'
#' # Turn image into BW
#' test %>%
#' image_threshold(type = "white", threshold = "50%") %>%
#' test |>
#' image_threshold(type = "white", threshold = "50%") |>
#' image_threshold(type = "black", threshold = "50%")
#'
#' # adaptive thresholding
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ image_scale(frink, "200x200")
image_flip(frink)
image_rotate(frink, 45) ## <-- result of this is shown
image_negate(frink)
frink %>%
image_background("green") %>%
image_flatten() %>%
frink |>
image_background("green") |>
image_flatten() |>
image_border("red", "10x10")
```

```r
image_rotate(frink, 45) %>% image_write("man/figures/frink-rotated.png")
image_rotate(frink, 45) |> image_write("man/figures/frink-rotated.png")
```

![](man/figures/frink-rotated.png)
Expand All @@ -65,7 +65,7 @@ image_edge(frink)
```

```r
image_charcoal(frink) %>% image_write("man/figures/frink-charcoal.png")
image_charcoal(frink) |> image_write("man/figures/frink-charcoal.png")
```

![](man/figures/frink-charcoal.png)
Expand Down
4 changes: 2 additions & 2 deletions man/edges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions man/morphology.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/segmentation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/thresholding.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 15 additions & 22 deletions vignettes/intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,7 @@ Note that size is currently 0 because ImageMagick is lazy (in the good sense) an

IDE's with a built-in web browser (such as RStudio) automatically display magick images in the viewer. This results in a neat interactive image editing environment.

<img id="rstudioimg" alt="rstudio">
<script>
//this is a hack to prevent pandoc 'self_contained' from embedding this image
//in future version of pandoc we can use the image and set 'data-external=1' instead
window.onload = function(){
document.getElementById("rstudioimg").src = "https://jeroen.github.io/images/magick-rstudio.png";
}
</script>
<img alt="rstudio screenshot" data-external=1 src = "https://jeroen.github.io/images/magick-rstudio.png">

Alternatively, on Linux you can use `image_display` to preview the image in an X11 window. Finally `image_browse` opens the image in your system's default application for a given type.

Expand Down Expand Up @@ -215,8 +208,8 @@ image_append(c(img, img_blurred))
Or use any of the [standard kernels](https://legacy.imagemagick.org/Usage/convolve/)

```{r}
img %>% image_convolve('Sobel') %>% image_negate()
img %>% image_convolve('DoG:0,0,2') %>% image_negate()
img |> image_convolve('Sobel') |> image_negate()
img |> image_convolve('DoG:0,0,2') |> image_negate()
```

### Text annotation
Expand Down Expand Up @@ -258,13 +251,13 @@ test <- image_annotate(test, "This is how we combine transformations", color = "
print(test)
```

Using `magrittr` pipe syntax makes it a bit more readable
Using pipe syntax makes it a bit more readable

```{r}
image_read("https://jeroen.github.io/images/frink.png") %>%
image_rotate(270) %>%
image_background("blue", flatten = TRUE) %>%
image_border("red", "10x10") %>%
image_read("https://jeroen.github.io/images/frink.png") |>
image_rotate(270) |>
image_background("blue", flatten = TRUE) |>
image_border("red", "10x10") |>
image_annotate("The same thing with pipes", color = "white", size = 30)
```

Expand All @@ -277,16 +270,16 @@ The standard base methods `[` `[[`, `c()` and `length()` are used to manipulate

```{r}
# Download earth gif and make it a bit smaller for vignette
earth <- image_read("https://jeroen.github.io/images/earth.gif") %>%
image_scale("200x") %>%
earth <- image_read("https://jeroen.github.io/images/earth.gif") |>
image_scale("200x") |>
image_quantize(128)
length(earth)
earth
head(image_info(earth))
rev(earth) %>%
image_flip() %>%
rev(earth) |>
image_flip() |>
image_annotate("meanwhile in Australia", size = 20, color = "white")
```

Expand Down Expand Up @@ -367,9 +360,9 @@ Morphing creates a sequence of `n` images that gradually morph one image into an
```{r}
newlogo <- image_scale(image_read("https://jeroen.github.io/images/Rlogo.png"))
oldlogo <- image_scale(image_read("https://jeroen.github.io/images/Rlogo-old.png"))
image_resize(c(oldlogo, newlogo), '200x150!') %>%
image_background('white') %>%
image_morph() %>%
image_resize(c(oldlogo, newlogo), '200x150!') |>
image_background('white') |>
image_morph() |>
image_animate(optimize = TRUE)
```

Expand Down

0 comments on commit ba4c28b

Please sign in to comment.