[英]How do I rescale local images for bookdown / rmarkdown website?
I have a pretty big (~14MB) *.jpeg
in my bookdown project (or rmarkdown website, doesn't really matter, I think). 我的预订项目(或者是rmarkdown网站,我认为这并不重要)我有一个非常大的(~14MB) *.jpeg
。 This is an external, static image, not touched by R (so far). 这是一个外部的静态图像,没有被R触及(到目前为止)。
I'm calling the picture like so: 我这样称呼这样的图片:
```{r q-pic, echo=FALSE, out.width="100%", fig.cap="Q-Sorting during the 2016 CiviCon", dpi = 72}
include_graphics(path = "img/q-sorting3.jpg")
```
I've also set retina via opts_knit$set(fig.retina = 2)
. 我也通过opts_knit$set(fig.retina = 2)
设置了视网膜。
I don't really care how huge the PDF is, but obviously, a ~14MB image on a website is pretty bad. 我真的不在乎PDF有多大,但显然, 网站上的~14MB图像非常糟糕。
Is there a way that some element of the knitr()
rmarkdown()
bookdown()
toolchain can automatically rescale images to a specified, appropriate resolution? 有没有一种方法, knitr()
rmarkdown()
bookdown()
工具链的某些元素可以自动将图像重新缩放到指定的适当分辨率?
I naively assumed that if both out.width
and dpi
were specified, that the image would be rescaled (ie.: smaller file size) behind the curtains, but that either appears not to be the case, or I'm using it wrong . 我天真地假设,如果同时指定了 out.width
和dpi
,那么图像将在窗帘后面重新缩放(即:较小的文件大小),但是看起来不是这种情况, 或者我使用它错了 。
Ps.: I understand that there's a possibility to specifiy a dpi
and then have knitr
figure out an appropriate size; Ps。:我知道有可能指定一个dpi
然后让knitr
找出合适的大小; that's not my concern. 那不是我关心的问题。 I'd like, sort of, the inverse of that. 我想,那种,那倒数 。
I think the only way to adjust the actual image size (and not just how it is scaled in the HTML) is to load the image into R and rasterize it: 我认为调整实际图像大小(而不仅仅是如何在HTML中缩放)的唯一方法是将图像加载到R并对其进行栅格化:
```{r fig.width=3}
library(jpeg)
library(grid)
img <- readJPEG("test.jpg")
grid.raster(img)
```
(Rasterization approach adapted from: How to set size for local image using knitr for markdown? ) (光栅化方法改编自: 如何使用knitr为缩进设置本地图像的大小? )
This will result in a smaller image/HTML file. 这将导致较小的图像/ HTML文件。
I've now also implemented a compression and re-scaling function in plain R. It's not fast, an it may be clumsy, but it get's the job done. 我现在还在简单的R中实现了压缩和重新缩放功能。它不是很快,它可能很笨拙,但它完成了工作。
library(jpeg)
library(imager)
resize_n_compress <- function(file_in, file_out, xmax = 1920, quality = 0.7, cutoff = 100000) {
# xmax <- 1920 # y pixel max
# quality <- 0.7 # passed on to jpeg::writeJPEG()
# cutoff <- 100000 # files smaller than this will not be touched
# file_out <- "test.jpg"
if (file.size(file_in) < cutoff) { # in this case, just copy file
if (!(file_in == file_out)) {
file.copy(from = file_in, to = file_out, overwrite = TRUE)
}
} else {# if larger than cutoff
image_raw <- imager::load.image(file = file_in)
if (dim(image_raw)[1] > xmax) { # resize only when larger
x_pix <- xmax # just in case we want to calculate this, too at some point
x_factor <- xmax/dim(image_raw)[1]
y_pix <- round(dim(image_raw)[2] * x_factor)
image_resized <- imager::resize(im = image_raw, size_x = x_pix, size_y = y_pix)
} else {# otherwise take raw
image_resized <- image_raw
}
saveme <- imager:::convert.im.toPNG(A = image_resized)
jpeg::writeJPEG(image = saveme, target = file_out, quality = quality) # this overwrites by default
}
}
Also see these related issues on knitr and blogdown . 还可以在knitr和blogdown上查看这些相关问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.