【问题标题】:How to trim a picture with rmarkdown/html?如何使用 rmarkdown/html 修剪图片?
【发布时间】:2018-01-11 09:21:32
【问题描述】:

我想在我的 Markdown 文档中包含来自网络的图片,但我只想要图片的左侧部分。我搜索了如何使用 rmarkdown 修剪图片,但一无所获...

这是一个例子

---
title: "How to trim?"
output: html_document
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE, fig.align = 'center')
```

Include picture

```{r pic}
include_graphics("http://ggplot2.tidyverse.org/README-example-1.png")
```

这给了我这个 HTML 输出。

如果我想修剪图例(右侧约 20% 的部分),我该怎么做?

我接受任何类型的答案:相对或绝对规范、rmarkdown 或 html 解决方案,...

谢谢!

【问题讨论】:

  • 查看magick 包。
  • 谢谢@hrbrmstr!我使用这个包发布了一个答案

标签: html r image r-markdown


【解决方案1】:

你可以使用:

library(magick)
crop <- function(im, left = 0, top = 0, right = 0, bottom = 0) {
  d <- dim(im[[1]]); w <- d[2]; h <- d[3]
  image_crop(im, glue::glue("{w-left-right}x{h-top-bottom}+{left}+{top}"))
}
"http://ggplot2.tidyverse.org/README-example-1.png" %>%
  image_read() %>%
  crop(right = 210)

【讨论】:

  • 很好地使用glue() 使magick 的字符串化参数非常易读。
【解决方案2】:

感谢@hrbrmstr 的评论,我找到了解决方案。

library(magick)
library(magrittr)
image_read("http://ggplot2.tidyverse.org/README-example-1.png") %>% 
  image_flop() %>% 
  image_crop("1344x960+250") %>% 
  image_flop()

我不确定这两个 image_flop() 函数是否最有效,我不完全理解 "1344x960+250" 但它有效 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2021-09-12
    • 1970-01-01
    • 2016-03-03
    • 2013-10-03
    • 2018-06-17
    相关资源
    最近更新 更多